首页 > 解决方案 > 我为99瓶歌曲制作了一个程序,但它没有正确打印歌曲

问题描述

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

每次墙上的瓶子运行时都会打印这条线,我不知道如何修复它。
else: print("墙上没有啤酒瓶了,没有啤酒瓶了。") print("去商店再买点,墙上有99瓶啤酒。") print()

def main(): 范围内的啤酒 (99,0,-1): printLyrics(beer)

主要的()

标签: pythonpython-3.x

解决方案


第一组打印应以 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)

推荐阅读