从python中输入的数字中的特定数字开始倒计时

时间:2013-03-22 20:38:47

标签: python

基本上我想让var2使瓶子下降那个数字,例如当我键入10然后2我想要瓶子倒计时2s像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.
    """

1 个答案:

答案 0 :(得分:2)

您需要做的就是减去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:

所以,如果你用92运行它,因为你最终得到-1瓶而不是0,这最后一节不会被打印出来。那是你要的吗? (此外,当你降到1,然后拿2,它将打印这节经文“拿下2并将它传递到墙上,-1瓶啤酒。”但这听起来像是人们或在化学增强最少的数学新生 - 实际上可能会唱歌。)