'Boredless Tourist' Problem of Empty Lists

Hey all,

I am following the course for ‘The Boredless Tourist’

I’m stuck at item 49, where I’m expecting it to output the result shown in the guide, ['LACMA', ['art', 'museum']], but instead I am getting an empty list.

My code, at least the important part, is as follows:

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)

  return attractions_with_interest

la_arts = find_attractions("Los Angeles, USA", ["art"])
print(la_arts)

I even followed along the video guide to the letter, and my code matches exactly with the instructor’s, but I cannot seem to successfully print the attractions_with_interest list with the possible_attraction appended into it.

The attractions list prints as expected, with ['LACMA', ['art', 'museum']] properly listed as an attraction in the sub-list of the main destination list.

I’m at a loss as to what I’m missing, any help would be appreciated.

I think we will have to see the full code to figure out the problem. maybe it’s just a typo in add_attraction("Los Angeles, USA", ["LACMA", ["art", "museum"]]) .

also, I’m wondering what’s the reason for this line?
possible_attraction = attraction

Yes, I suppose that’s fair :slight_smile:

The possible_attraction = attraction is a temporary variable that’s specified in the guide, with the instructions as:

  1. Create a new list called attractions_with_interest . Make it empty when declaring it, we’ll save attractions into this list if they match one of our interests.
  2. Create a loop over attractions_in_city saving each item in the list into the temporary variable possible_attraction.

Here’s the full code:

destinations = ["Paris, France", "Shanghai, China", "Los Angeles, USA", "São Paulo, Brazil", "Cairo, Egypt"]

test_traveler = ["Erin Wilkes", "Shanghai, China", ["historical site", "art"]]

def get_destination_index(destination):
  destination_index = destinations.index(destination)
  return destination_index

#print(get_destination_index("Paris, France"))

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 destination in destinations:
  attractions.append([])

#print(attractions)

def add_attraction(destination, attraction):
  try:
    destination_index = get_destination_index(destination)
    attractions_for_destination = attractions[destination_index].append(attraction)
  except ValueError:
     print("Destination does not exist!")

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", "historcical 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("São Paulo, Brazil", ["São Paulo Zoo", ["zoo"]])
add_attraction("São 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)

  return attractions_with_interest

la_arts = find_attractions("Los Angeles, USA", ["art"])
print(la_arts)
1 Like
attraction_tags = attractions[1]

the tag is the second element in every attraction, not the whole attractions list. so just remove the s and change it to

attraction_tags = attraction[1]

======================
about the temporary variable, attraction here is the temporary variable which stores every attraction during the loop. if you want to name it possible_attraction just do it directly there is no reason for doing it in a separate line, so you can write it like that:

for possible_attraction in attractions_in_city:
    attraction_tags = possible_attraction[1]

Just to expand a little bit on this:

you’ve defined the global variable as “attractions” in the line:
attractions =

so when you loop through this code:

for attraction in attractions_in_city:
    possible_attraction = attraction
    attraction_tags = attractions[1]

you are indexing into the global variable not the “loop variable (attraction)”

Thanks everyone! Of course, all the suggestions worked :slight_smile: I should have figured it was as simple as a typo.

Frustratingly, I did actually have my temporary variable as possible_attraction in my for loop originally, and did not have the troublesome possible_attraction = attraction - but I changed it after watching the walkthrough video. I think that might need a review.