从python中输入的特定数字开始倒计时
基本上,我希望var2使瓶子下降到那个数字,例如,当我输入10,然后2时,我希望瓶子在2秒内倒数,比如10 8 6 4 2 0从python中输入的特定数字开始倒计时,python,Python,基本上,我希望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 = ""
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.
"""
你所需要做的就是减去
瓶子
,而不是1
。就这样
当然,您可能也想更改文本,因此它会显示“Take”,bottle\u down,“down”
,而不是“Take one down”
,或者输出有点混乱
同时
没有理由这样做:
countdown = bottle_number - bottle_down
bottle_number = countdown
只要做:
bottle_number = bottle_number - bottle_down
或:
没有理由在顶部初始化倒计时=“
”。你从不使用这个值,为什么要存储它呢
同时,与其计算两次瓶号-瓶号向下
(一次在打印
语句中,然后再次存储以供下一个循环),为什么不在打印
语句之前只计算一次呢
最后,在最后,你得到:
if bottle_number == 0:
因此,如果你用
9
和2
运行它,因为你最终得到的是-1瓶而不是0瓶,最后一节不会被打印出来。这就是你想要的吗?(还有,当你降到1,然后拿2,它会打印一句诗“拿2下来,把它传给周围,-1瓶啤酒挂在墙上。”但这听起来像是人们或者至少是化学强化数学新生可能会唱的歌。)@cdhowie:我不确定这是否重要。他解释了这个问题,他向我们展示了他所做的努力(这显然是一个坚实的努力),这使它成为一个好问题
if bottle_number == 0: