Stuck on The Boredless tourist python3 project

Hello, all.
I’m currently on task 48 of The Boredless Tourist python3 project, linked here, and I’m a bit stuck. I have completed all previous tasks fine, and have had no errors, yet when I execute the following code, instead of it returning “[‘LACMA’, [‘art’, ‘museum’]]]”, it returns an empty set of square brackets.

destinations=["Paris, France", "Shanghai, China", "Los Angeles, USA", "Sao Paulo, Brazil", "Cairo, Egypt"] test_traveler=['Erun Wilkes', 'Shanghai, China', ['historical site', 'art']] def get_destination_index(destination): destination_index=destinations.index(destination) return destination_index def get_traveler_location(traveler): traveler_destination=traveler[1] traveler_destination_index=get_destination_index(traveler_destination) return traveler_destination_index test_destination_index=get_traveler_location(test_traveler) print(test_destination_index) attractions=[[] for item in destinations] def add_attraction(destination, attraction): destination_index=get_destination_index(destination) attractions_for_destination=attractions[destination_index].append(attraction) return attractions_for_destination add_attraction("Los Angeles, USA", ['Venice Beach', ['beach']]) add_attraction("Paris, France", ["the Louvre", ["art", "museum"]]) add_attraction("Paris, France", ["Arc de Triomphe", ["historical site", "monument"]]) add_attraction("Shanghai, China", ["Yu Garden", ["garden", "historical site"]]) add_attraction("Shanghai, China", ["Yuz Museum", ["art", "museum"]]) add_attraction("Shanghai, China", ["Oriental Pearl Tower", ["skyscraper", "viewing deck"]]) add_attraction("Los Angeles, USA", ["LACMA", ["art", "museum"]]) add_attraction("Sao Paulo, Brazil", ["Sao Paulo Zoo", ["zoo"]]) add_attraction("Sao Paulo, Brazil", ["Pátio do Colégio", ["historical site"]]) add_attraction("Cairo, Egypt", ["Pyramids of Giza", ["monument", "historical site"]]) add_attraction("Cairo, Egypt", ["Egyptian Museum", ["museum"]]) print(attractions) def find_attractions(destination, interests): destination_index=get_destination_index(destination) attractions_in_city=attractions[destination_index] attractions_with_interest=[] for attraction in attractions_in_city: possible_attraction=attraction attraction_tags=attractions[1] for interest in interests: if interest in attraction_tags: attractions_with_interest.append(possible_attraction) else: pass return attractions_with_interest #Below is the code that isn't printing what it should# la_arts=find_attractions("Los Angeles, USA", ['art']) print(la_arts)

Can anyone see any obvious errors, misspellings or incorrect variables?
Thank you in advance, all help is appreciated.

The problem lies in this line of the find_attractions function:

attraction_tags=attractions[1]

Just before this function, you have the statement

print(attractions)

Print statements are very useful for debugging, and for inspecting the data and its structure. If you can’t figure out problems with your code, it is a good idea to use print statements to see what is happening at different points of your program. They can be commented out easily, and once you are satisfied that your program is working properly, you can delete the print statements used for debugging.

Your print statement gives the structure of the data assigned to the attractions variable. To make the structure clearer, have a look at the screenshot:

Within the attractions list, there are 5 lists (red arrows) corresponding to the 5 destinations.
Within each destination list, there are lists corresponding to an individual attraction associated with at destination (blue arrows).
Within each attraction list, there are two elements. The first element is the name of the attraction (orange arrow). The second element is a list (purple) which holds the tags assigned to that attraction.

Within your find_attractions function,

attractions_in_city=attractions[destination_index]

targets one of the 5 lists (red arrows) corresponding to the desired destination. It would be a good idea to write a print statement for debugging purposes so that you can see exactly what you are working with.

attractions_in_city=attractions[destination_index]
attractions_with_interest=[]
print(attractions_in_city)  # You can delete/comment out once done with debugging
for attraction in attractions_in_city:

In each iteration of the for loop, the loop variable attraction will be assigned one of the blue arrow lists. You want to search through the tags (purple list).
You wrote:

attraction_tags=attractions[1]

This targets the second red list of the attractions list. That’s not what you want, is it?

You want to target the purple list (second element of the blue list). In every iteration of the for loop,

for attraction in attractions_in_city:

a blue list is assigned to the loop variable attraction.
I think you should now be able to figure out how to target the purple list of that blue list and assign it to attraction_tags.

If still unclear and/or debugging print statements aren’t helpful, see spoiler:

Spoiler
# You wrote:
attraction_tags=attractions[1]

# Change it to:
attraction_tags=possible_attraction[1]

# Since you have written the statement possible_attraction=attraction,
# so alternatively attraction_tags=attraction[1] will also work.
1 Like