【问题标题】:TypeError when printing the result from input()从 input() 打印结果时出现 TypeError
【发布时间】:2013-09-22 04:51:24
【问题描述】:

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

【问题讨论】:

    标签: python string python-3.x


    【解决方案1】:

    正确的语法:

    >>> 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'
    

    【讨论】:

      猜你喜欢
      • 2019-12-09
      • 1970-01-01
      • 2017-05-15
      • 1970-01-01
      • 1970-01-01
      • 2014-01-06
      • 1970-01-01
      • 2019-07-05
      • 1970-01-01
      相关资源
      最近更新 更多