2013-06-20 50 views
2

只是試圖創建一個元組來添加我的主要編程。反正這是我的代碼 -TypeError:只能連接元組(不是「str」)到元組錯誤

print"I have a few things to finish my exam, but i might need more" 
exam=("Brain","Computer","python") 
print "The stuff i have are:" 
for stuff in exam: 
    print stuff 
print"I still need my previous assignments!" 
extra=("Assignments") 
exam += extra 
for stuff in exam: 
    print stuff 

我不斷收到只能拼接元組的錯誤。任何人都有線索我的問題/如何解決它?不勝感激。

+0

未來,格式化你的代碼會讓你的代碼嘗試更容易閱讀,這樣人們就可以清楚地看到你想要做什麼以及在哪裏他的問題是 – xgord

+0

非常感謝!我試圖自己做,但無法得到它的工作,只是想出瞭如何格式化它。 –

回答

10

那麼這是因爲根據python doc

Tuples are constructed by the comma operator (not within square brackets), with or without enclosing parentheses, but an empty tuple must have the enclosing parentheses, such as a, b, c or(). A single item tuple must have a trailing comma, such as (d,).

所以,如果你這樣做你的代碼它的工作

extra = "Assignments", 

extra = ("Assignments",) 
+0

嘿,我只是試過,非常感謝你! –

相關問題