COMP 141: Project 5
Bottles of a Beverage
You will write a program to sing the children's (or maybe young adult)
song about bottles
of a beverage on a wall, with a twist. You may use whatever beverage you want in the song,
just keep it PG-13; I don't want any parents calling me asking why I'm encouraging underage
drinking or anything like that.
How the program should act
- The user will be prompted to enter how many bottles start on the wall, as well as how many bottles
to take off the wall at the time.
- Input validation will be used to ensure the user enters only positive numbers for these values.
- Then the program will print the lyrics to the song, using the user's numeric values.
- Then the program will ask the user if they want to sing another version of the song. If the user
says yes (with a capital or lowercase 'y') then the program will again ask the user for input and sing the song again. This cycle
repeats over and over until the user answers No, they don't want to sing the song anymore.
What you need to do
Part A
Part A will just do one time
through the whole song, from start to finish. Part B will handle the loop for repeating the
song again with different numbers.
Start by writing a program that will prompt the user to enter how many bottles they
wish to start with on the wall, as well as how many bottles to take off the wall at a time.
-
Both of these values should be checked to make sure the user enters numbers that
are greater than zero for both variables (input validation).
- If the user enters an invalid number
for either value, print an error message and ask them to re-enter the number.
- Do not wait
until both variables have been entered to do the checking --- if the first
variable entered is negative (for how many bottles to start with), ask them to
re-enter it until they get it right before going on to the variable for how many
to take off the wall each time.
- Hint: I suggest writing an input validation function
like we did in class when we learned this concept. Then your code can call this
function to accept only positive numbers.
Then print each verse of the song, taking care that the number of bottles on the wall
never becomes negative (it may go to zero, but not below). You do not need to worry
about making the song grammatically correct (changing "bottles" to "bottle" or "them"
to "it." That's a challenge problem --- see below).
You may assume the user will enter a whole number (an integer), not a float or a string.
However, as it says in the paragraph above, if they enter a negative integer or zero, you must
ask them to re-enter it until they fix it.
Test Part A
Before moving on to Part B, I suggest rigorously testing Part A. Make sure your program
acts like the outputs below, just without the part that asks the user if they want to sing
the song again.
Part B
Now, add a message at the very end of your program --- after the lyrics are printed ---
that asks the user if they want to sing another version of the song. If the user answers
Yes or yes, then the program should ask for new versions of the starting
number of bottles and number of bottles to remove each time, and print the lyrics to the
new song. Keep doing this in a loop, over and over, until the user answers anything other than yes.
Hint for Part B
I suggest turning what you wrote for Part A into a function that you can call from Part B. In other words,
like we often do in class, consider renaming your existing main() function to something else,
and then writing a new main() function that calls it inside a loop. This way you don't have to
write a loop inside of a loop.
Test Part B
At this point, you are done, and your program should act similarly to mine below. Test your program
thoroughly.
Guidelines
You may design your program in any way you see fit, though you should follow
the guidelines for good programming that we've discussed in class: using comments, choosing
appropriate variable names, making use of functions where appropriate, etc.
In particular, be sure to follow the commenting guidelines.
Sample Interaction
(What the user types is in bold.)
How many bottles should we start with? 0
You must start with a positive number of bottles. Please re-enter the number.
How many bottles should we start with? -88
You must start with a positive number of bottles. Please re-enter the number.
How many bottles should we start with? 10
How many do we take off the wall each time? -1
You must remove a positive number of bottles from the wall. Please re-enter the number.
How many do we take off the wall each time? 0
You must remove a positive number of bottles from the wall. Please re-enter the number.
How many do we take off the wall each time? 1
10 bottles of pop on the wall, 10 bottles of pop.
Take 1 down, pass them around, 9 bottles of pop on the wall.
9 bottles of pop on the wall, 9 bottles of pop.
Take 1 down, pass them around, 8 bottles of pop on the wall.
8 bottles of pop on the wall, 8 bottles of pop.
Take 1 down, pass them around, 7 bottles of pop on the wall.
7 bottles of pop on the wall, 7 bottles of pop.
Take 1 down, pass them around, 6 bottles of pop on the wall.
6 bottles of pop on the wall, 6 bottles of pop.
Take 1 down, pass them around, 5 bottles of pop on the wall.
5 bottles of pop on the wall, 5 bottles of pop.
Take 1 down, pass them around, 4 bottles of pop on the wall.
4 bottles of pop on the wall, 4 bottles of pop.
Take 1 down, pass them around, 3 bottles of pop on the wall.
3 bottles of pop on the wall, 3 bottles of pop.
Take 1 down, pass them around, 2 bottles of pop on the wall.
2 bottles of pop on the wall, 2 bottles of pop.
Take 1 down, pass them around, 1 bottles of pop on the wall.
1 bottles of pop on the wall, 1 bottles of pop.
Take 1 down, pass them around, 0 bottles of pop on the wall.
Would you like to sing another version of the song? Yes
How many bottles should we start with? 8
How many do we take off the wall each time? 2
8 bottles of pop on the wall, 8 bottles of pop.
Take 2 down, pass them around, 6 bottles of pop on the wall.
6 bottles of pop on the wall, 6 bottles of pop.
Take 2 down, pass them around, 4 bottles of pop on the wall.
4 bottles of pop on the wall, 4 bottles of pop.
Take 2 down, pass them around, 2 bottles of pop on the wall.
2 bottles of pop on the wall, 2 bottles of pop.
Take 2 down, pass them around, 0 bottles of pop on the wall.
Would you like to sing another version of the song? yes
How many bottles should we start with? 10
How many do we take off the wall each time? 3
10 bottles of pop on the wall, 10 bottles of pop.
Take 3 down, pass them around, 7 bottles of pop on the wall.
7 bottles of pop on the wall, 7 bottles of pop.
Take 3 down, pass them around, 4 bottles of pop on the wall.
4 bottles of pop on the wall, 4 bottles of pop.
Take 3 down, pass them around, 1 bottles of pop on the wall.
Notice how the last run stops at 1 bottle on the wall, because
with taking three down each time, we have to stop there because
we don't want to have -2 bottles on the wall.
Your code does not have to follow this exact script verbatim, but all the
mentioned functionality should be there: asking the user for number of bottles
and how many to remove each time, and printing each verse in turn, stopping
before hitting a negative number.
What to turn in
Through Moodle, turn in your code as a file called bottles_yourLastName_yourFirstName.py.
Challenge Problems
- Make the lyrics grammatically correct for the situations where there
is only one bottle on the wall and where the user is removing a single bottle
each time.
-
Make your program print the numbers using words instead of digits. For instance,
instead of printing "10 bottles of pop on the wall..." your program would print
"Ten bottles of pop on the wall..." You may assume the number of bottles on the wall
will never be greater than 99. Don't do this with 99 if statements --- that's not the
point of this challenge. Hint: The concept of string concatenation
(see section 3.1 in the zyBook) may be useful, though there are certainly ways to do this
challenge without it.
- Using simplegraphics, draw an animation to accompany your song, illustrating
the bottles disappearing from the wall. You could do this by drawing something,
clearing the canvas, then drawing something else, or you could just hide the bottles
by drawing a shape over top of them so they "disappear" on the canvas.
Feel free to be creative. If you find the
animation goes too quickly, you can use the function time.sleep(seconds) to pause
your program for a given number of seconds (which may be fractional). Just put
import time at the top of your program. Then, for example, you can call
time.sleep(0.5) to pause the program for half a second.