Writing a Song

Tony wants to print out the full lyrics to the song "99 Bottles of Beer on the Wall," so he's written the code below. The song is supposed to involve taking down each of the 99 bottles in turn and passing them around until all the bottles have been taken down and passed around.

1
2
3
4
5
6
7
8
9
number = 99
while number > 1:
# each loop prints one verse of the song
    print `number` + " bottles of beer on the wall." 
    print `number` + " bottles of beer on the wall." 
    print "Take one down; pass it around." 
    number = number - 1
    print `number` + " bottles of beer on the wall." 
    print " "

What is wrong with his lyrics?

Image Credit: Darien Graham-Smith
The song will have verses missing in the middle of the song Nothing; this will print the correct number of verses The song will have extra verses after all the bottles have been used The song runs out of verses before the bottles are all used

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

12 solutions

Finn Hulse
Mar 27, 2014

Notice Line 2. This states that the code will only run if the number of beers is greater than one. So the program stops when there's still a bottle of beer on the wall. :D

Daniel Lim
Apr 1, 2014

Once the number of bottles is not greater than one then it will stop

So it should be either,

1
2
3
4
5
6
7
8
9
number=99
    while number >=1:
    # each loop prints one verse of the song
        print `number` + " bottles of beer on the wall." 
        print `number` + " bottles of beer on the wall." 
        print "Take one down; pass it around." 
        number = number -1
        print `number` + " bottles of beer on the wall." 
        print " "

or

1
2
3
4
5
6
7
8
9
number=99
    while number > 0:
    # each loop prints one verse of the song
        print `number` + " bottles of beer on the wall." 
        print `number` + " bottles of beer on the wall." 
        print "Take one down; pass it around." 
        number = number -1
        print `number` + " bottles of beer on the wall." 
        print " "

Wrong! That code won't print these verses: (bottle, without s, for a single bottle. if number = 1:) 1 bottle of beer on the wall, 1 bottle of beer. Take one down and pass it around, no more bottles of beer on the wall.

(when all the bottles are taken down. if number = 0:) No more bottles of beer on the wall, no more bottles of beer. Go to the store and buy some more, 99 bottles of beer on the wall.

Alger Coronado - 7 years ago

Honestly that initial code is alright, but needs some additional information. However it should NOT say "while number > 0" because then the last verse would be "1 bottles of beer on the wall" and that is not correct English. The code is simply missing the end of the song.

Sanyo Neezy - 2 years ago
Thaddeus Abiy
Mar 27, 2014
1
2
#The program terminates before n=1
print "The new syntax highlighting feature is awesome!"

Eddie The Head
Apr 5, 2014

The while loop says the number pf beer bottles should be >1 ..As a result when the number of beer bottles is the loop is not entered and hence the last bottle is not removed....Hence The song runs out of verses before the bottles are all used .

Ihtesham Ul Haq
Mar 31, 2014

The condition should be "while number >= 1" in order to run for 99 times, the loop in the problem runs 98 times so verses ends before all bottles are used.

Personally, I would find it more logical to use >0 rather than >=1 .

Eric Kim - 7 years, 2 months ago
Vighnesh Raut
Mar 30, 2014

condition given in the program is n is greater than 1. But, in the question , we want all the bottles to be passed on . So, the verses runt out after n becomes equal to 2 .

the loop that is used in this code never break or it is always in running condition because its a infinte loop butt in the syntax when number become equal to the 1 the loop will closed

Sheikh Talha Ejaz - 7 years, 2 months ago

Log in to reply

I agree,the loop will continually go and it will closed because of that syntax.

Eric Barrameda - 7 years, 2 months ago
Youssef Hassan F
Dec 18, 2015

number >1 is wrong it must be number >0 i have not read all of the code i have read only first two lines

Kristian Iroy
May 18, 2014

The loop will end after the counter reaches 98 as counter 99 will not meet the criteria for the lyrics to go on.

Just open the lyrics of the song and you'll get it that the last verse cant be printed when the bottles are down to 0.

OBOE- off by one error Test it by plugging in the number two. That would be the last time it loops until the condition is false. Also, reading the answer choices, the some of the others would seem unlikely.

Just open the lyrics of the song and you'll get it that the last verse cant be printed when the bottles are down to 0.

Sanjay Kamath
Mar 31, 2014

loop should be >=0 (simple) The loop should be decremented till ALL the bottles are accounted for. This is clearly not the case in the example given.

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...