The Boredless Tourist can't concanetate tuple to str error

def get_attractions_for_traveler(traveler):
traveler_destination = traveler[1]
traveler_interests = traveler[2]
traveler_attractions = find_attractions(traveler_destination, traveler_interests)
interests_string = "Hi " + traveler[0], "we think you’ll like these places around " + traveler_destination + ": "
for i in range(len(traveler_attractions)):
if traveler_attractions[-1] == traveler_attractions[i]:
interests_string += "the " + traveler_attractions[i] + ". "
interests_string += "the " + traveler_attractions[i] + ", "
return interest_strings

I am getting an error in this section of code after re-writing ins several times and looking at the walk through video and literally copying it word for word and it is giving me this can’t concanetate str to tuple error and I have no idea what is going on. The walkthrough didn’t get this error and I literally copied in from the walkthrough. Very confused and wondering if it is a bug. Hopefully y’all can help. This is the exact error File “”, line 73, in get_attractions_for_traveler
interests_string += "the " + traveler_attractions[i] + ". "
TypeError: can only concatenate tuple (not “str”) to tuple

Given the error message, where is the tuple, and where is the str?

>>> (1, 2, 3) + (4, 5, 6)
(1, 2, 3, 4, 5, 6)

What this suggests is that either, interests_string is a tuple, or traveler_attractions[i] is one. The opposing one is seen as a string. All this really tells us is to check the data type of the variables we are using and be sure they correlate.

Just learned from the error message that while tuples cannot be mutated, they can be extended. NB.

1 Like

So basically I have to go check the variables and make sure both line up to be the same type? I’m wondering if I accidentally created a tuple somewhere above in the variable by using () instead of square brackets and maybe that is why it is getting confused.

Unless you can spot your error by eye then yes hunting down the variable type as @mtf suggested would be a very good idea, things like the print and type functions can help here. If they’re not what you expect them to be in the line shown in your traceback then start working backwards from there until you find the problem.

Using print or similar to track it down would be easiest, for fixing the line it’s worth noting that pantheses are often used for representing tuples when output as a string and such but it’s not strictly parentheses that make a tuple. Have a look for the following syntax but be aware that many built-ins return tuples by default (it is python’s basic sequence data structure).

x = 1, 2, 3   # tuple
y = (1, 2, 3,)  # tuple
print(x == y)

There’s an exception to that syntax for an empty tuple but it’s not something you’d need to worry about much, tuple() == ().

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.