首页 > 解决方案 > Python中的瓶子程序

问题描述

我正在使用 Python 在线进行 Bottle Program 作为练习练习,尽管我被困在最后一步,即歌曲其余部分的倒计时,但我已经成功完成了练习。

例如:如果我输入'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")

标签: python

解决方案


首先,您:在第一行忘记了a。

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

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)

推荐阅读