2013-09-22 109 views
1

我正在使用python 3.3,我試圖使用%s函數,但出現錯誤。我把 print("you're a %s man") % (input("put an adjective"))打印輸入結果時出現TypeError()

然後我在模塊中運行它,並得到這個

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'

回答

5

正確的語法:

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

print()回報None在py3.x,所以你試圖將字符串格式應用到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' 
+0

謝謝你的快速回復。現在它工作了! – user2801425