Home > Mobile >  I made a program for the 99 bottles song, but it doesn't print the song correctly
I made a program for the 99 bottles song, but it doesn't print the song correctly

Time:11-17

def printLyrics(beer):
    print(str(beer)   " bottles of beer on the wall, "   str(beer)   " bottles of beer")
    print("Take one down and pass it around "   str(beer-1)    " bottles of beer on the wall.")
    print()


    if beer == 2:
        print("2 bottles of beer on the wall, 2 bottles of beer.")
        print("Take one down and pass it around, 1 bottle of beer on the wall.")
        print()    
    elif beer == 1:
        print("1 bottle of beer on the wall, 1 bottle of beer.")
        print("Take one down and pass it around, no more bottles of beer on the wall.")
        print()

This line prints every time bottles on the wall runs, I don't know how to fix it.
else: print("No more bottles of beer on the wall, no more bottles of beer.") print("Go to the store and buy some more, 99 bottles of beer on the wall.") print()

def main(): for beer in range(99,0,-1): printLyrics(beer)

main()

CodePudding user response:

The first set of prints should be conditional to beer > 2.

def printLyrics(beer):
    if beer>2:
        print(str(beer)   " bottles of beer on the wall, "   str(beer)   " bottles of beer")
        print("Take one down and pass it around "   str(beer-1)    " bottles of beer on the wall.")
        print()
    elif beer == 2:
        print("2 bottles of beer on the wall, 2 bottles of beer.")
        print("Take one down and pass it around, 1 bottle of beer on the wall.")
        print()    
    else:
        print("1 bottle of beer on the wall, 1 bottle of beer.")
        print("Take one down and pass it around, no more bottles of beer on the wall.")
        print()

for beer in range(99,0,-1):
    printLyrics(beer)

CodePudding user response:

I was thinking the same thing as Alain.

def print_lyrics(beer):
    if beer == 2:
        print("2 bottles of beer on the wall, 2 bottles of beer.")
        print("Take one down and pass it around, 1 bottle of beer on the wall.")
        print()    
    elif beer == 1:
        print("1 bottle of beer on the wall, 1 bottle of beer.")
        print("Take one down and pass it around, no more bottles of beer on the wall.")
        print()
    elif beer == 0:
        print("No more bottles of beer on the wall, no more bottles of beer.")
        print("Go to the store and buy some more, 99 bottles of beer on the wall.")
        print()
    else:
        print(str(beer)   " bottles of beer on the wall, "   str(beer)   " bottles of beer")
        print("Take one down and pass it around "   str(beer-1)    " bottles of beer on the wall.")
        print()


for beer in range(99, -1, -1):
    print_lyrics(beer)
  • Related