Boredless tourist: TypeError

Hi! So i am doing this project and I also followed the project walkthrough but as soon as I run the program it shows error on the second line of the code mentioned below. it says ‘Type error: must be str not list’. what does this mean? I don’t know the issue. Please help.

if traveler_attractions[-1] == traveler_attractions[i]:

  interests_string += "the " + traveler_attractions + "." 

else:
  interests_string += "the " + traveler_attractions + ", "

return interests_string

Link to exercise: https://www.codecademy.com/paths/computer-science/tracks/cspath-cumulative-tourism/modules/cspath-boredless-tourist/projects/the-boredless-tourist

Take a look at the error; it explains that a list was unexpectedly encountered rather than a string. When we concatenate strings, all the of the objects being concatenated must be of the str data type.

Take a closer look at "the " + traveler_attractions + "." and "the " + traveler_attractions + ", ". Are all of the parts being concatenated of the str data type? Note: this problem would be solved if the program was written to meet the requirements of the exercise; continue reading below.

Additionally, what’s the goal of this program? You’re trying to add each element in traveler_attractions to interests_string. Right now, you’re attempting to add the entire list all at once. What changes do you need to make?

Side note: in future posts, please format your code using the </> button.

3 Likes

Thanks Victoria!

The list contains a number of attractions, so does it not automatically become a string?

I have the for loop running before the if statement. Followed the project walkthrough completely. But still don’t understand the problem…

The list is a list of attractions correct? Even if all the elements inside the list (i.e. the attractions) are strings, the entire list itself is still of the list data type.

Take a look at the following line.

interests_string += "the " + traveler_attractions + "." 

What does this do?

It tries to concatenate the entire list. Essentially, you are trying to do the following.

interests_string += "the " + ["The Louvre", "Arc de Triomphe", "Champs-Élysées"] + "."

This won’t work since only str data types can be concatenated. We only want to concatenate a single element at a time, not the entire list. How can we do this? Hint: You can use indexing to access an element from a list.

1 Like
interests_string += "the " + **str**(traveler_attractions) + "." 

By adding ‘str’, the element of the list will change into a string. Right?

traveler_attractions is a list, so str(traveler_attractions) returns the traveler_attractions list formatted as a string:

a = ["w", "w"]
print(str(a))
#gives ["w", "w"] output
1 Like