Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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()

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading