python – 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

我一直得到的只能连接元组错误.任何人都有一个线索我的问题/如何解决它?非常感激.

解决方法:

那是因为根据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",)
上一篇:Python字符串神奇地转换为元组.为什么?


下一篇:Python:count直到列表中的元素是一个元组