Boredless Toursit

estinations = ["Paris, France", "Shanghai, China", "Los Angeles, USA", "Sao 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("Hyderbad, India"))

def get_traveler_location(traveler):
  traveler_destination = traveler[1]
  traveler_destination_index = get_destination_index (traveler_destination)
  return traveler_destination_index

attractions = []
for destination in destinations:
  attractions.append([])

def add_attraction(destination, attraction):
  try:
    destination_index = get_destination_index(destination)
    attractions_for_destination = attractions[destination_index].append(attraction)
  except SyntaxError:
    return

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", ["São 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"]])

def find_attractions(destination, interests):
  destination_index = get_destination_index(destination)
  attractions_in_city = attractions[destination_index]
  attractions_with_interest = []

  for attractions in attractions_in_city:
    possible_attraction = attraction
    attraction_tags = attraction[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)

When I go to run the code I get this Error: Traceback (most recent call last):
File “script.py”, line 53, in
la_arts = find_attractions(“Los Angeles, USA”, [‘art’])
File “script.py”, line 41, in find_attractions
attractions_in_city = attractions[destination_index]
UnboundLocalError: local variable ‘attractions’ referenced before assignment

I followed the video exactly after not being able to figure it out and still can’t get rid of this error.

yes, sometimes the functions don’t access variables in the global namespace/scope

One way to fix fix that is to use the global keyword:

In the find_attractions function,
on a line before you use attractions,
put:
global attractions

Also, in the for-loop later in that function,
there seems to be a typo
in
for attractions in attractions_in_city:
Here, attractions should be attraction
because that’s what you’re using inside that loop.

I changed the code in the for-loop from “attractions” to “attraction” and problem solved! I spent too much time analyzing and I guess missed a simple letter…thank you for the help!