带循环的Python程序协助

时间:2019-04-05 20:22:14

标签: python

我必须编写一个程序,该程序将100瓶啤酒中的歌曲放在墙上,并一路计数,直到不再剩一瓶。我可以使代码正常工作,但是当代码达到1时,我遇到了麻烦。我希望它说的是“瓶”而不是“瓶”;当代码达到0时,我希望它说“不再装啤酒”。墙”。这是我到目前为止的内容:

def main():

    for x in range (10, 0, -1):

        print(str(x), "Bottles of beer on the wall", str(x), " bottles of beer take one down, pass it arround, " , end="")

    if x == str(1):
        print("bottle of beer on the wall," , "bottle of beer, take it down bass it around")
    if x  == 0:
        print("no more bottles of beer on the Wall",)
    else:
        print("")

main()

当x达到1时只是“瓶”,而x达到0时即“墙上不再有啤酒瓶”,输出的预期结果

2 个答案:

答案 0 :(得分:0)

 for x in range (10, 0, -1):

    print(str(x), "Bottles of beer on the wall", str(x), " bottles of beer take one down, pass it arround, " , end="")

    if x == 1:
        print("bottle of beer on the wall," , "bottle of beer, take it down bass it around")
    elif x  == 0:
        print("no more bottles of beer on the Wall",)
    else:
        print("")

答案 1 :(得分:-1)

if语句应该在循环中,因为循环中x的值可以是1或零:

for x in range (10, 0, -1):
    if x == str(1):
         print("bottle of beer on the wall," , "bottle of beer, take it down bass it around")
    if x  == 0:
        print("no more bottles of beer on the Wall",)
    else:
         print(str(x), "Bottles of beer on the wall", str(x), " bottles of beer take one down, pass it arround, " , end="")