Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

tuples

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.

like image 585
Mark Nguyen Avatar asked Jun 20 '13 00:06

Mark Nguyen


1 Answers

Well that is because according to the 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,).

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

extra = "Assignments", 

or

extra = ("Assignments",)
like image 84
Victor Castillo Torres Avatar answered Oct 05 '22 03:10

Victor Castillo Torres