Python TypeError:只能将元组(不是“str”)连接到元组错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17203690/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 00:45:30  来源:igfitidea点击:

TypeError: can only concatenate tuple (not "str") to tuple Error

pythontuples

提问by Mark Nguyen

Just trying to make a tuple to add to make my main programming. Anyway this is my code-

只是想制作一个元组来添加以制作我的主要程序。无论如何,这是我的代码-

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


I keep getting the can only concatenate tuple error. Anyone have a clue my issue/how to fix it? Greatly appreciated.

我不断收到 can only concatenate tuple 错误。任何人都知道我的问题/如何解决它?不胜感激。

采纳答案by Victor Castillo Torres

Well that is because according to the python doc

那是因为根据python 文档

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,).

元组由逗号运算符(不在方括号内)构造,带或不带括号,但空元组必须有括号,例如 a、b、c 或 ()。单项元组必须有一个尾随逗号,例如 (d,)。

so if you do this to your code it has to work

所以如果你对你的代码这样做,它必须工作

extra = "Assignments", 

or

或者

extra = ("Assignments",)