2015-09-02 137 views
-4

我對此很新,所以我不知道爲什麼這是錯誤的。請幫忙。我不知道爲什麼我的Python代碼不能正常工作

謝謝,如果你幫忙。

print("%s is a very %s person. They usually spend all their time %s.") % input("Please enter a name: "), input("Please enter an adjective: "), input("Please eneter an ing verb: ") 

這是錯誤我得到:

Traceback (most recent call last): 
    File "C:\Python34\firstthingy.py", line 1, in <module> 
    print("%s is a very %s person. They usually spend all their time %s.") % input("Please enter a name: "), input("Please enter an adjective: "), input("Please eneter an ing verb: ") 
TypeError: unsupported operand type(s) for %: 'NoneType' and 'str' 

回答

0

如果您使用python2然後使用的raw_input(),而不是輸入()

print("{0} is a very {1} person. They usually spend all their time {2}.".format(input("Please enter a name: "), input("Please enter an adjective: "), input("Please eneter an ing verb: "))) 
0

分裂您的代碼行成小塊,看哪一個拋出錯誤,並從那裏去。

錯誤是告訴你它不能處理Nonestr對象之間的模操作。

確保您在使用模數前一直處理字符串。

2

你錯位了括號。如果字符串中有多個%格式化項目,則必須將參數包裝在括號(一個元組)中。

print("%s is a very %s person. They usually spend all their time %s." % (input("Please enter a name: "), input("Please enter an adjective: "), input("Please eneter an ing verb: "))) 

這是一個正在運行的版本。確保你花時間瞭解使用的括號。

相關問題