TypeError:没有足够的参数用于格式化字符串-使用While循环

2024-04-29 14:03:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在做旧的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

我试过在%后面的“瓶子,s1”周围加上括号,但还是没有用。有什么建议吗?


Tags: ofthe代码numberifonnot歌曲