


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



Files to submit: hw4.py Functions to include in hw4.py:
Recall the recursive function for multiply: def mult(a, b): if b == 0: return 0 if b == 1: return a else: return a + mult(a, b-1) Now write a function called multiply() that calculates the product of two non-negative integers using a while loop. Your function should NOT print anything – it should return the answer with an actual return statement. WARNING! Do NOT use the * operator or recursion for this or you will receive no credit for this function. You must use a while loop! Also be sure your function handles 0. Sample output:
multiply(4, 2)
8
multiply(0, 85) 0 multiply(14, 23) 322
Write a function that uses a while loop to sum the numbers from 1 through n. For example, summation(4) = 4 + 3 + 2 + 1 or 1 + 2 + 3 + 4 = 10 Your function must return the result. (No printing.) If you do not use a while loop for the calculation you will get no credit. Sample output:
summation(4) 10 summation(31) 496
Write a function that returns the sum of the digits of the integer parameter using a while loop. For example, digitSum(143) = 3 + 4 + 1 = 8 HINT: The modulus operator (%) is your friend! Recall that a number mod 10 gives you the last digit and the number divided by 10 (integer division) gives you the other digits (without that last one). Sample Output:
digitSum(4) 4 digitSum(14) 5 digitSum(987654) 39 If you do not use a while loop you will get no credit.
Remember how to compute the average of a set of numbers: average = sum of numbers / count of numbers What is a sentinel value? In computer programming, a sentinel value (also referred to as a flag value or signal value) is a
Enter the next number to average (type q to quit): 53. Enter the next number to average (type q to quit): 474. Enter the next number to average (type q to quit): - Enter the next number to average (type q to quit): 747 Enter the next number to average (type q to quit): 983. Enter the next number to average (type q to quit): 100. Enter the next number to average (type q to quit): q Average: 376.
You are planning a party where the main attraction is a fabulous piñata. You need to fill the piñata with enough candy so that all of your guests reach near satiation (3 pieces of candy per person or slightly more). Prompt the user for the number of guests attending the party. Using a while loop, while the total amount of candy is less than three times the people attending, keep asking the user to put more candy in the piñata. Pretend that the size or amount of the amount of candy is proportional to the length of the name of the candy inputted. (See the example runs below to make sense of this ) Let the user know how much more candy they need to put in the piñata before it is full. Once the piñata is full, let the user know and print out all the different types of candy in the piñata. Sample output:
pinata() How many people are at the party? 5 You still need 15 pieces of candy to fill the pinata What kind of candy would you like to put in the pinata? Snickers You still need 7 pieces of candy to fill the pinata What kind of candy would you like to put in the pinata? Gum You still need 4 pieces of candy to fill the pinata What kind of candy would you like to put in the pinata? Powerbars The pinata is full! You filled the pinata with: Snickers Gum Powerbars
Be sure to name your files and functions exactly as indicated. READ THROUGH THE INSTRUCTIONS CAREFULLY. If you need help, be sure to come to office hours. Good Luck! Written/Adapted by Beth York, Spring 2008