Starting from:

$20

Returning Values and Simple Conditions

1 Part I: Function Writing Drills (70 points) 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). Each function description shows several examples of calling the function. Your function should work properly with the arguments/input used in those examples and any other acceptable values (each function description gives the accepted type of value for each parameter and/or input). If your function only works on the values shown in the examples it is wrong. There are no specific requirements on what you should put in main to test each function. It is fine to comment out the test code you have in main for functions you’ve already finished. Create a new file called “conditions.py”. In the file write the following functions (10 points each): 1.) This function requires a bit of preparation first: Try entering each of the following in a Python shell: len('mumps') len('elk') len('') phrase = 'That is silly!' length = len(phrase) print(length) Using what you just learned about the len function, write a function called word length that takes two parameters. The first will be a string and the second will be an integer. The function first prints a message about the relationship between the length of the word and the integer, as shown in the following examples: • Here are some examples of calling the function (code executed is in blue, output is in green): word length('liversnaps', 7) Longer than 7 characters: liversnaps word length('earwax', 5) Longer than 5 characters: earwax word length('chickenfat', 10) Exactly 10 characters: chickenfat word length('Gross!', 13) Shorter than 13 characters: Gross! 2 • Write code in main to test this function. Don’t just test it on the arguments given in the examples. Test it on enough different values to convince yourself that it works properly. 2.) Write a function called stop light that determines the color that a stop light should change to. It takes two parameters. The first will either be “green”, “yellow”, or “red”. This represents the color that the stop light is currently showing. The second parameter is how long this color has been showing. If green has been showing longer than 60 seconds, print “yellow”. If yellow has been showing longer than 5 seconds, print “red”. If red has been showing longer than 55 seconds, print “green”. If the color hasn’t been showing long enough (e.g. green has been showing for 17 seconds), print the current color. • Here are some examples of calling the function (code executed is in blue, output is in green): stop light('green', 61) yellow stop light('yellow', 5) yellow stop light('yellow', 6) red stop light('red', 12) red stop light('red', 56) green • Write code in main to test this function. Don’t just test it on the arguments given in the examples. Test it on enough different values to convince yourself that it works properly. 3.) Write a function called is normal blood pressure that takes two integer parameters. The first represents systolic blood pressure (the top number in a blood pressure reading). The second represents diastolic blood pressure (the bottom number in a blood pressure reading). The function should return True if systolic is less than 120 AND diastolic is less than 80 (i.e. blood pressure is normal). Otherwise it returns False. • Here are some examples of calling the function (code executed is in blue, returned values are in rust): blood pressure(120, 80) False 3 blood pressure(119, 80) False blood pressure(119, 79) True blood pressure(120, 79) False • Write code in main to test this function. Don’t just test it on the arguments given in the examples. Test it on enough different values to convince yourself that it works properly. 4.) Write a function called doctor that takes no parameters. The function will ask the user to enter his/her systolic blood pressure reading. It will then ask for the diastolic reading. The function then prints either “Your blood pressure is normal.” or “Your blood pressure is high.” depending on the values entered. This function should use the function you wrote in the previous question. • Here are some examples of calling the function (code executed is in blue, output is in green, user input is in turquoise): doctor() Enter your systolic reading: 119 Enter your diastolic reading: 79 Your blood pressure is normal. doctor() Enter your systolic reading: 133 Enter your diastolic reading: 79 Your blood pressure is high. • Make sure you test the function on a variety of input. Don’t just test it on the values used in the examples. Test it on enough different values to convince yourself that it works properly. 5.) Write a function called pants size that takes a single parameter (the value will be an integer) representing a person’s waist size in inches. The function returns a string. The string returned will be either “small”, “medium”, or “large” depending on the parameter value. Waist measurements that are 34 inches or larger should return large. Measurements that are 30 inches or larger, but not large enough to be in the large category, should return medium. Anything smaller should return small. • Here are some examples of calling the function (code executed is in blue, returned values are in rust): pants size(38) large 4 pants size(34) large pants size(33) medium pants size(29) small pants size(-20) small pants size(2000) large • Write code in main to test this function. Don’t just test it on the arguments given in the examples. Test it on enough different values to convince yourself that it works properly. 6.) Write a function called pants fitter that takes no parameters. The function should first ask the user for his/her name. It then greets the user by name. Next it asks the user for his/her waist size in inches (a positive integer). It then asks the user how many pairs of pants he/she would like to buy (a positive integer). Next it asks what type of pants the user wants to buy (either “regular” or “fancy”). Next it calculates the cost of the pants (integer). Regular pants cost $40 per pair. Fancy pants cost $100 per pair. Finally it prints out the number of pairs, the size, the type, and the total cost. The following examples show the format that your prompts and output should be in. This function should use the function you wrote in the previous question. • Here are some examples of calling the function (code executed is in blue, output is in green, user input is in turquoise): pants fitter() Enter your name: Ziggy Greetings Ziggy welcome to Pants-R-Us Enter your waist size in inches: 34 How many pairs of pants would you like? 2 Would you like regular or fancy pants? fancy 2 pairs of large fancy pants: $ 200 pants fitter() Enter your name: Elmer Greetings Elmer welcome to Pants-R-Us Enter your waist size in inches: 31 How many pairs of pants would you like? 10 Would you like regular or fancy pants? regular 10 pairs of medium regular pants: $ 400 5 pants fitter() Enter your name: Minnie Greetings Minnie welcome to Pants-R-Us Enter your waist size in inches: 12 How many pairs of pants would you like? 1 Would you like regular or fancy pants? fancy 1 pairs of small fancy pants: $ 100 • Make sure you test the function on a variety of input. Don’t just test it on the values used in the examples. Test it on enough different values to convince yourself that it works properly. 7.) Write a function called digdug that takes a single parameter number (assume it will always be a positive integer). For every integer from 1 up to and including number, the function will print a message. If the integer is evenly divisible by 3 the function will print “dig”. If the integer is evenly divisible by 5 it prints “dug”. If the integer is evenly divisible by both 3 and 5 it prints “digdug”. If the integer is not divisible by either 3 or 5 it does not print anything. • Here are some examples of calling the function (code executed is in blue, output is in green): digdug(1) digdug(2) digdug(3) 3 : dig digdug(5) 3 : dig 5 : dug digdug(15) 3 : dig 5 : dug 6 : dig 9 : dig 10 : dug 12 : dig 15 : digdug • Write code in main to test this function. Don’t just test it on the arguments given in the examples. Test it on enough different values to convince yourself that it works properly. 6 2 Part II: Fix It (30 points) Download the file called “pluses.py”. This Python program is broken. When you run the program you will find a syntax error. 1.) Your first task is to fix all of the syntax and run-time errors in the program: i.) Run the program and read the error message. ii.) Find and fix the error. iii.) Within the documentation string at the top of the file where it says ‘ADD YOUR LIST OF BUG FIXES HERE’ add an explanation of what the bug was and how you fixed it. iv.) Now go back to step (i.) and repeat the process. v.) Continue running the program and finding and fixing bugs until there are no more errors. • Make sure you add a note to the bug list explaining each bug you fix. vi.) When you have fixed the program it will draw the pattern shown in the following figure: 7 *** IMPORTANT: DO NOT GO ON TO THE NEXT TASK UNTIL YOU HAVE COMPLETELY FINISHED DEBUGGING THE CODE. 2.) Once the program is error free, your next task is to improve the program using what you have learned about Python • Think about the style conventions we’ve learned (documentation, variable/parameter/function naming) • Think about why/when we should use for-loops, functions, variables, parameters • In the documentation string at the top of the file where it says ‘ADD YOUR LIST OF IMPROVEMENTS HERE’ list each of your improvements along with an explanation of why it is an improvement. 3.) Verify that your documentation makes sense and that you’ve added documentation to each of your functions. 4.) Verify that your program works 5.) Upload your file to the Program 4 dropbox folder on D2L

More products