The Boredless Tourist

When i do python3 script.py
with this code it says

"File "script.py", line 47, in find_attractions attractions_in_city = attractions[destination_index] UnboundLocalError: local varible 'attractions' referenced before assignment"
and i’m having trouble finding a way to fix it.
if anyone can help me with this issue it would be much appreciated

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 int(destination_index)

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

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

print(traveler_destination_index)

attractions =[[] for destination in destinations]
print(attractions)

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

add_attraction("Los Angeles, USA", ['Venice Beach',['beach']])

print(attractions)

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"]])

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

 for attractions in attractions_in_city:
    possible_attaction = attractions
    attraction_tags = possible_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)

Should attractions actually be, attractions_for_destination?

4 Likes

that doesn’t seem to be the solution

What error message are you getting now?

attractions_for_destination is not defined

Hi @vipercc

The error is in this function definition:

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

 for attractions in attractions_in_city:
    possible_attaction = attractions
    attraction_tags = possible_attraction[1]
    
    for interest in interests:
      if interest in attraction_tags:
        attractions_with_interest.append(possible_attraction)
       
        return attractions_with_interest

The traceback is telling you that you have referenced a variable - that is to say you’re trying to read the value stored in it - before you’ve assigned a value to it.

Let’s flip that around, and you might be able to spot the error.

Can you see anywhere that you might have declared a variable attractions after referencing it?

(Edit: There are other issues in your program which will prevent it from running correctly, but let’s concentrate on the one causing the Traceback in question for now. :slight_smile: )

4 Likes

All those long variable names are twisting my brain. I get them, but they are clutter, just the same.

Have been going from scratch to reconstruct what I deduce from your code. Here’s the very start…

destinations = ["Paris, France", "Shanghai, China", "Los Angeles, USA", u"S\xe3o Paulo, Brazil", "Cairo, Egypt"]

attractions = [[destination] for destination in destinations]

print (attractions)

def display(a):
    for m in a:
        print (m[0])
>>> ================================ RESTART ================================
>>> 
[['Paris, France'], ['Shanghai, China'], ['Los Angeles, USA'], [u'S\xe3o Paulo, Brazil'], ['Cairo, Egypt']]
>>> display(attractions)
Paris, France
Shanghai, China
Los Angeles, USA
São Paulo, Brazil
Cairo, Egypt
>>> 

I’m on my old laptop in Python 2. The Unicode encoding is not needed in Python 3.

Going to continue in this vein so may not have an answer for awhile. Keep digging in the meantime.


Observable from the above is that we have initialized the first element in each member list of the attractions list.

4 Likes