1

我正在使用 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'

4

1 回答 1

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'
于 2013-09-22T04:53:20.537 回答