python - 类型错误 : not enough arguments for format string - using a While Loop

标签 python

我正在创作古老的 99 Bottles 歌曲,并尝试使用 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”周围使用括号,但仍然没有帮助。有什么建议吗?

最佳答案

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

print "%d %s of beer on the wall," % (bottles, s1)

关于python - 类型错误 : not enough arguments for format string - using a While Loop,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13896612/

相关文章:

python - 保存/加载字典,同时保留键的数据类型

python - 使用python脚本通过telnet在远程主机上执行命令

python - boost::python 参数类型不匹配

python - 如何在 Flask 模板中显示当前年份?

python - 如何从非声明性对象自动生成 SQLAlchemy 架构?

python - 在 python 中构建对象列表以进行矢量化 : Can a list of structures(objects) be vectorized, 或者需要显式数组

python - 可以设置Python对象的任何属性

Python:如何通过 xml-rpc 从文件 block 中获取数据

python - cygwin 路径使用。我就是想不明白

python - 使用字典查找 DataFrame 变量的值