python - 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")
解决方案
首先,您:
在第一行忘记了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)
推荐阅读
- c - 在 Python 中调用的 C 函数中的编辑距离不返回正确的结果
- python - 如何删除csv文件中的不可打印字符?
- python - 比这个嵌套的 try-except 语句更好的方法?
- java - Spring Redis pubsub:onMessage 回调数据操作不起作用
- authentication - PayPal和支付实施
- windows - 批处理文件删除旧文件
- python - 从 DateTime Pandas 系列中提取小时数
- c# - C# WPF 线程:如何在事件函数中停止新创建的线程(单击按钮)。不影响主线程
- java - 在 weka jython 中处理数据属性
- sql-delete - SQL“DELETE FROM ...”不起作用,因为弹性池已满