1

基本上我希望 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.
    """
4

1 回答 1

2

您需要做的就是减去bottle_down而不是1. 而已。

当然,您可能也想更改文本,所以它说"Take", bottle_down, "down"而不是"Take one down",否则输出有点混乱。

同时……</p>

没有理由这样做:

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:

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

于 2013-03-22T20:41:27.440 回答