基本上我希望 var2 使瓶子下降该数字,例如当我输入 10 然后 2 时,我希望瓶子在 2 秒内倒计时,例如 10 8 6 4 2 0
var = raw_input(" Enter bottle number ")
var2 = raw_input(" Enter how many bottle taken down ")
bottle_number = int(var)
bottle_down = int(var2)
countdown = ""
while bottle_number > 0:
print bottle_number, "bottles of beer on the wall,", bottle_number, "bottles of beer."
print "Take one down and pass it around,", bottle_number - 1, "bottles of beer on the wall."
print " "
countdown = bottle_number - 1
bottle_number = countdown
if bottle_number == 0:
print """
No more bottles of beer on the wall, no more bottles of beer.
"""
最佳答案
您所需要做的就是减去 bottle_down
而不是 1
。就是这样。
当然,您可能也想更改文本,因此它显示“Take”,bottle_down,“down”
而不是“Take one down”
,或者输出有点困惑。
同时……
没有理由这样做:
countdown = bottle_number - bottle_down
bottle_number = countdown
就这样做:
bottle_number = bottle_number - bottle_down
或者:
bottle_number -= bottle_down
并且没有理由在顶部初始化 countdown = ""
。您从不使用该值,那么为什么要存储它呢?
同时,与其计算 bottle_number - Bottle_down
两次(一次在 print
语句中,然后再次存储以供下一个循环使用),为什么不只执行一次,在 print
语句之前?
最后,你得到了:
if bottle_number == 0:
因此,如果您使用 9
和 2
运行它,因为您最终会得到 -1 个瓶子而不是 0 个瓶子,所以最后一节不会被打印。那是你要的吗? (另外,当你减少到 1 并取 2 时,它会打印这样的诗句“将 2 取下来并传递给周围,-1 瓶啤酒放在墙上。”但这听起来像是人们或在最不擅长化学强化的数学新生——可能真的会唱歌。)
关于python - 从Python中输入的数字中的特定数字开始倒数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15579461/