CS 1301 - Spring 2008 Homework 4 - Python, the Party Planner by odq14517

VIEWS: 37 PAGES: 4

									CS 1301 – Spring 2008
Homework 4 – Python, the Party Planner
Due: Friday, February 15, 2008 at 11:55 PM EST

Files to submit: hw4.py

Functions to include in hw4.py:
   1. multiply()
   2. summation()
   3. digitSum()
   4. average()
   5. piñata()

For Help:
    T-Square Forum
    TA Helpdesk – schedule is posted on class website
    Email/IM TAs

Notes:
    Think through the logic of each of the problems before you start coding anything
    Don’t forget to include the required comments and collaboration statement (as outlined
       on the course syllabus)
    Do not wait until the last minute to do this assignment in case you run into problems
    If you find an error in the homework assignment, let a TA or Monica know immediately

Part 1 – Multiply
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)

Homework 4                           CS 1301 – Spring 2008                             Page 1/4
8
>>> multiply(0, 85)
0
>>> multiply(14, 23)
322


Part 2 – Summation
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


Part 3 – Digit Sum
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.

Part 4 – Average
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

Homework 4                           CS 1301 – Spring 2008                                Page 2/4
special value that is used to terminate a loop that processes structured (especially sequential)
data. The value should be selected in such a way that it will not be confused with legal data
values.
A programmer may choose any sentinel value, but here are some typical examples:
-999
xxx
FALSE
END
QUIT
q
Write a function that keeps asking the user for numbers until they enter the letter q instead of a
number. Print out the final average of all of the numbers.
Use floats for all numbers!
Your program must work even if they choose q immediately. If the user enters q as the first
input, the function should print:

No average could be computed.
(Or something similar) instead of trying to divide by zero. (That would give an error in python.)

You will need to use a while loop to add the numbers together. Do NOT use recursion.
Here is a simple algorithm you can use:
1. Initialize any variables you will need.
2. Ask the user for input (It is very important to ask the user for the first number before
       starting the loop)!
3. While the input does not equal the sentinel value
       a. Update any variables that need to change
       b. Ask for the next Number
       c. Keep looping
4. If more than 0 numbers have been entered, compute the average and print it.
5. Otherwise, print: No average could be computed.

Sample output:
>>> average()
Enter the next number to average (type q to quit): q
No average could be computed.

>>> average()
Enter the next     number     to   average   (type   q   to   quit):   10.0
Enter the next     number     to   average   (type   q   to   quit):   20.0
Enter the next     number     to   average   (type   q   to   quit):   30.0
Enter the next     number     to   average   (type   q   to   quit):   q
Average: 20.0

>>> average()
Enter the next number to average (type q to quit): 0
Enter the next number to average (type q to quit): q
Average: 0.0

>>> average()


Homework 4                              CS 1301 – Spring 2008                               Page 3/4
Enter the next number      to   average   (type   q   to   quit):   53.643
Enter the next number      to   average   (type   q   to   quit):   474.9
Enter the next number      to   average   (type   q   to   quit):   -100
Enter the next number      to   average   (type   q   to   quit):   747
Enter the next number      to   average   (type   q   to   quit):   983.73
Enter the next number      to   average   (type   q   to   quit):   100.00
Enter the next number      to   average   (type   q   to   quit):   q
Average: 376.5455

Part 5 – Piñata Time!!
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



Final thoughts
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

Homework 4                           CS 1301 – Spring 2008                                Page 4/4

								
To top