Functions and Loops

1 Part I: Function Writing Drills
In this section you will write a number of functions that do not need turtle graphics. For each function you will write code in main to test the function and convince yourself it works properly (like we did in the fruit example in lecture). There are no speciﬁc requirements on what you should put in main to test each function. It is ﬁne to comment out the test code you have in main for functions you’ve already ﬁnished.
Create a new ﬁle called “functions.py”. In the ﬁle write the following functions (10 points each): 1.) Write a function called print word that takes two parameters. The ﬁrst is for an integer (assume it will always be non-negative). The second is for a string. The function will print the given string the given number of times each time preceded by a count. See the examples below for the format of the output (i.e. if you call your function with the same arguments as in the examples your output should look like the output in the examples). • Here are some examples of calling the function with diﬀerent arguments (The code executed is in blue, the output produced is in green): print word(3, 'banana') 1 -- banana 2 -- banana 3 -- banana
print word(4, 'mississippi') 1 -- mississippi 2 -- mississippi 3 -- mississippi 4 -- mississippi
• Note: look carefully at the output in the examples above. The function is printing more than just the string. • Write code in main to test this function. Don’t just test it on the arguments given in the examples. Test it on enough diﬀerent values to convince yourself that it works properly.
2.) Write a function called bacteria that will print out information showing the number of bacteria in a Petri dish at equal time intervals. The function takes two parameters. The ﬁrst is an integer giving the number of minutes it takes for a bacterium to split into two new bacteria. The second is an integer giving the number of bacterial generations to include in the output. Assume you always begin with a single bacterium in the dish and every bacterium always splits into exactly two bacteria at the end of each time period.
2
• Here are some examples of calling the function with diﬀerent arguments (The code executed is in blue, the output produced is in green): bacteria(10, 5) after 10 minutes: 2 bacteria after 20 minutes: 4 bacteria after 30 minutes: 8 bacteria after 40 minutes: 16 bacteria after 50 minutes: 32 bacteria
bacteria(21, 3) after 21 minutes: 2 bacteria after 42 minutes: 4 bacteria after 63 minutes: 8 bacteria
• Write code in main to test this function. Don’t just test it on the arguments given in the examples. Test it on enough diﬀerent values to convince yourself that it works properly.
3.) Write a function called convert to copper that takes three integer parameters. The ﬁrst represents a number of gold coins. The second represents a number of silver coins. The third represents a number of copper coins. The function will print the numbers of each type of coin followed by the total value of all of the coins when converted to copper. The exchange rate for coins1 is given in the following table:
10 copper pieces (cp) = 1 silver piece (sp) 20 silver pieces (sp) = 1 gold piece (gp)
• Here are some examples of calling the function with diﬀerent arguments (The code executed is in blue, the output produced is in green): convert to copper(5, 10, 7) 5 gp, 10 sp, 7 cp converted to copper is: 1107 cp
convert to copper(15, 23, 12) 15 gp, 23 sp, 12 cp converted to copper is: 3242 cp
• Write code in main to test this function. Don’t just test it on the arguments given in the examples. Test it on enough diﬀerent values to convince yourself that it works properly.
4.) Write a function called convert from copper that takes a single integer parameter representing a number of copper pieces. The function prints out
1Gygax, G. Players Handbook (1978)
3
the number of gold pieces (gp), silver pieces (sp), and copper pieces (cp) you would end up with if you ﬁrst converted as many of the initial copper pieces to gold as possible and then converted as many of the remaining copper pieces as possible to silver pieces. • Examples of calling the function with diﬀerent arguments (The code executed is in blue, the output produced is in green):
convert from copper(200) 200 copper pieces is: 1 gp, 0 sp, 0 cp
convert from copper(1107) 1107 copper pieces is: 5 gp, 10 sp, 7 cp
convert from copper(3242) 3242 copper pieces is: 16 gp, 4 sp, 2 cp
• Write code in main to test this function. Don’t just test it on the arguments given in the examples. Test it on enough diﬀerent values to convince yourself that it works properly.
5.) This function requires a bit of preparation ﬁrst:
Try entering each of the following in a Python shell:
print('Hobbit' * 10) print('Hobbit' * 2) print('Hobbit' * 1) print('Hobbit' * 0)
Using what you just learned, write a function called repeat word that has three parameters. The ﬁrst is for a word (a string). The second is for an integer representing a number of rows. The third is for an integer representing a number of columns. The function prints the word in a number of rows equal to the value of the rows parameter and each row contains the word repeated a number of times equal to the columns parameter, as shown in the examples below:
• Examples of calling the function with diﬀerent arguments (The code executed is in blue, the output produced is in green): repeat word('Goblin', 3, 5) GoblinGoblinGoblinGoblinGoblin GoblinGoblinGoblinGoblinGoblin GoblinGoblinGoblinGoblinGoblin
4
repeat word('Kobold', 5, 3) KoboldKoboldKobold KoboldKoboldKobold KoboldKoboldKobold KoboldKoboldKobold KoboldKoboldKobold
• Write code in main to test this function. Don’t just test it on the arguments given in the examples. Test it on enough diﬀerent values to convince yourself that it works properly.
6.) Using what you learned in the previous question, write a function called text triangle that takes an integer parameter and prints X’s in a triangle shape as seen in the following examples: • Examples of calling the function with diﬀerent arguments (The code executed is in blue, the output produced is in green): text triangle(3) X XX XXX
text triangle(10) X XX XXX XXXX XXXXX XXXXXX XXXXXXX XXXXXXXX XXXXXXXXX XXXXXXXXXX
• Hint: think about how many spaces you will need in front of each X. • Be careful, make sure you end up with the correct number of X’s! • Write code in main to test this function. Don’t just test it on the arguments given in the examples. Test it on enough diﬀerent values to convince yourself that it works properly.
7.) This function requires a bit of preparation ﬁrst:
In other programs we’ve used the turtle module (import turtle) to give us access to turtle graphics functions. The math module gives us access
5
to a number of useful math functions and constants. Add the following import statement to the top of your ﬁle (before your ﬁrst function deﬁnition):
import math
The math module has a variable called pi that contains the value of π (3.14...) carried out to many decimal places. Anywhere you need that value use: math.pi
Write a function called surface area of cylinder that takes two parameters. The ﬁrst is a ﬂoat representing the radius of a cylinder. The second is a ﬂoat representing the height of a cylinder. The function calculates and prints the surface area of a cylinder with the given radius and height. Recall from math: The area of a circle = πr2 (where r is the radius of the circle) The circumference of a circle = 2πr If you’re having trouble, imagine cutting the top and bottom oﬀ of a soup can. Then slice up the side of the can and lay it out ﬂat.
• Examples of calling the function with diﬀerent arguments (The code executed is in blue, the output produced is in green):
surface area of cylinder(10.0, 10.0) The surface area of a cylinder with radius 10 and height 10 is 1256.6370614359173
surface area of cylinder(3.0, 1.0) The surface area of a cylinder with radius 3 and height 1 is 75.39822368615503
surface area of cylinder(0.0, 10.0) The surface area of a cylinder with radius 0 and height 10 is 0.0
• Write code in main to test this function. Don’t just test it on the arguments given in the examples. Test it on enough diﬀerent values to convince yourself that it works properly.
8.) The math module has a function called sqrt that returns the square root of its parameter. It is used like this: math.sqrt(100).
6
Imagine you are ﬂying a kite and the kite gets caught in the top of a perfectly straight palm tree. The string pulls free from the kite leaving you with the full length of string and your kite stuck in the tree. You measure the distance from you to the base of the tree. Given the length of the kite string and the distance from you to the base of the tree you can calculate the height of the tree using the Pythagorean Theorem: a2 + b2 = c2 In this case a is the distance from you to the tree, b is the unknown height of the tree, and c is the length of the kite string.
Write a function called tree height that takes two parameters. The ﬁrst is a ﬂoat representing the distance from you to the base of the tree. The second is a ﬂoat representing the length of the kite string. The function will calculate and print the height of the tree as shown in the examples below. • Examples of calling the function with diﬀerent arguments (The code executed is in blue, the output produced is in green):
tree height(300, 500) Kite string: 500 Distance: 300 Height: 400.0
tree height(100, 141.421356) Kite string: 141.421356
7
Distance: 100 Height: 99.99999966439368
• Write code in main to test this function. Don’t just test it on the arguments given in the examples. Test it on enough diﬀerent values to convince yourself that it works properly.
9.) Note: since we are not using turtle graphics in this program your main function does not need: input('Enter to end') but you do still need the last two lines: if name == ' main ': main()
10.) Verify that your documentation makes sense and that you’ve added documentation to each of your functions.
11.) Verify that your program works
12.) Upload your ﬁle to the Program 3 dropbox folder on D2L
2 Part II: Shape Chooser (20 points)
Create a new ﬁle called “shapechooser.py”. In the ﬁle write a program that does the following:
1.) Ask the user how many polygons to draw.
2.) For each of the polygons perform the following steps:
i.) Ask the user how many sides the polygon should have. ii.) Ask the user what color to use. iii.) Ask the user for the length of a side of the polygon. iv.) Using the values you collected from the user’s input, set up the turtle and draw the desired polygon • Copy your polygon function from your last homework into this ﬁle
3.) Note: remember that user input will be a string, when you need a diﬀerent type you’ll need to convert
4.) Here’s an example run of the program (see below for screenshots taken after each shape is drawn) (Output is in green, user input is in turquoise):
8
Enter the number of polygons you’d like to see: 3 Enter the number of sides: 4 Enter the color: red Enter the side length for your polygon: 50
Enter the number of sides: 3 Enter the color: green Enter the side length for your polygon: 100
Enter the number of sides: 5 Enter the color: blue Enter the side length for your polygon: 200
Enter to end
5.) Here are screen shots taken during the example run:
9
10
11
6.) Verify that your documentation makes sense and that you’ve added documentation to each of your functions.
7.) Verify that your program works.