2

我正在做旧的 99 瓶歌曲并尝试使用 While 循环来完成它,以帮助我继续更好地学习这种循环类型。

我想知道为什么在下面的代码中会出现 TypeError 以及我缺少哪些参数?

这是我的代码:

# Get number of beers
bottles = int(raw_input("How many bottles of beer? "))

# return invalid response
if bottles < 1:
    print "That's not a good number"

    if bottles == 1:
        s1 = "bottle" 
        s2 = "bottles" 

    elif bottles == 2:
        s1 = "bottles" 
        s2 = "bottles" 

# sing verses
while bottles > 0:
    print "%d %s of beer on the wall," % bottles, s1
    print "%d %s of beer." % bottles, s1
    print "You take one down, pass it around,"
    print "%d %s of beer on the wall." % (bottles - 1), s2
    print
    bottles -= 1

这是错误:

Traceback (most recent call last):
    File "beer.py", line 47, in <module>
          print "%d %s of beer on the wall," % bottles, s1
TypeError: not enough arguments for format string

我尝试在 % 之后的“bottles,s1”周围使用括号,但仍然没有帮助。有什么建议么?

4

1 回答 1

5

您必须将多个参数指定为元组,例如

print "%d %s of beer on the wall," % (bottles, s1)
于 2012-12-15T22:01:42.720 回答