Python中的瓶子程序

2024-06-16 14:56:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用Python在线做一个瓶子程序作为练习,我已经成功地完成了这个练习,尽管我还停留在最后一步,那就是歌曲的剩余部分的倒计时。你知道吗

例如:如果我输入'4',它只显示:(4个绿色瓶子,挂在墙上4个绿色瓶子挂在墙上 如果一个绿色的瓶子,不小心会掉下来 会有3个绿色的瓶子,挂在墙上)

但我在想办法让它降到3,2,1然后结束。你知道吗

如果我进入“7”,那么它会从7降到1。你知道吗

我被困在哪里,我需要把这个纳入我的计划。你知道吗

def bottles(b)
    print(b,"green bottles, hanging on the wall",   
          b,"green bottles hanging on the wall")

bottleno = int(input("Enter number of bottles: "))
bottles(bottleno)
print("And if one green bottle, should accidentally fall")
print("There'd be", bottleno-1, "green bottles, hanging on the wall")

Tags: the程序瓶子ongreen歌曲小心print
1条回答
网友
1楼 · 发布于 2024-06-16 14:56:01

首先,你的第一行忘了一个:。你知道吗

您需要做的是在函数中有一个while循环,并将重复的print语句放入其中,如下所示:

def bottles(b):
  i = b
  while (i > 0):

    print(i,"green bottles, hanging on the wall",   
    i,"green bottles hanging on the wall")

    print("And if one green bottle, should accidentally fall")
    print("There'd be", i-1, "green bottles, hanging on the wall")

    i -= 1


bottleno = int(input("Enter number of bottles: "))
bottles(bottleno)

相关问题 更多 >