Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Writing a Program in Python 99 Bottles of Beer on the Wall Your assignment is to

ID: 3679945 • Letter: W

Question

Writing a Program in Python

99 Bottles of Beer on the Wall

Your assignment is to write a program that sings (i.e. prints) the road trip song, "99 Bottles of Beer".

To quote Wikipedia's entry about this song,

"99 Bottles of Beer" is a traditional song in the United States and Canada. It is popular to sing on long trips, as it has a very repetitive format which is easy to memorize, and can take a long time to sing. In particular the song is frequently sung by children on long bus trips, such as class field trips, or on Scout and/or Girl Guide outings.

Depending on how long the road trip is, students (i.e. users), must decide how many bottles of beer to start with. So the program should ask for that input. The program should then proceed to output lyrics from the song until there are no more bottles of beer on the wall.

Example input/output

Some notes on your program:

"One bottle" is singular, while "Two bottles" is plural. This grammar should be correctly output by your program.

The last verse says, "no more bottles of beer on the wall!" instead of "0 bottles of beer on the wall."

If a user enters an invalid response to the question, "How many bottles of beer on the wall?", the program must ask the question again and again until a proper number is given in response.

Explanation / Answer

n = 0
while(True):
print "How many bottles of beer on the wall?"
n = int(raw_input())
if(n > 0) :
print
break
print "Sorry, that's not a valid number of bottles. Try again."
print
while (n!=2) :
print n," bottles of beer on the wall, ",n," bottles of beer."
print "Take one down, pass it around, ",n-1," bottles of beer on the wall."
n = n-1
if(n == 2):
print n," bottles of beer on the wall, ",n," bottles of beer."
print "Take one down, pass it around, ",n-1," bottle of beer on the wall."
n = n-1
if(n == 1):
print n," bottle of beer on the wall, ",n," bottle of beer."
print "Take it down, pass it around, no more bottles of beer on the wall."