从input()打印结果时出现TypeError

时间:2013-09-22 04:51:25

标签: python string python-3.x

我正在使用python 3.3而我正在尝试使用%s函数,但是我收到了一个错误。我放 print("you're a %s man") % (input("put an adjective"))

然后我在模块中运行它并获得此

you're a %s man put an adjectivefunny Traceback (most recent call last): File "C:\Users\Me\Desktop\coding\mie.py", line 1, in <module> print("you're a %s man") % (input("put an adjective")) TypeError: unsupported operand type(s) for %: 'NoneType' and 'str'

1 个答案:

答案 0 :(得分:5)

正确的语法:

>>> print("you're a %s man" % input("put an adjective: "))
put an adjective: foo
you're a foo man

print()在py3.x中返回None,因此您尝试将字符串格式应用于None

>>> None % "foo"
Traceback (most recent call last):
  File "<ipython-input-2-1aa4f0c0cbbd>", line 1, in <module>
    None % "foo"
TypeError: unsupported operand type(s) for %: 'NoneType' and 'str'