The Boredless Tourist #35

destinations = [‘Paris, France’, ‘Shanghai, China’, ‘Los Angeles, USA’, ‘Sao Paulo, Brazil’, ‘Cairo, Egypt’]
test_traveler = [‘Erin Wilkes’, ‘Sao Paulo, Brazil’, [‘historical site’, ‘art’]]
attractions = [ for i in destinations]

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

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

test_destination_index = get_traveler_location(test_traveler)
print(test_destination_index)
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(“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”]])

I have a question about how the function def add_attraction works. The function appends attraction to list attractions in the beginning of the code. It’s a list of five empty lists. The function first looks for destination index based on the location input. So Paris, France is 0, LA is 3 etc etc. I get this part. Then, we create a variable, attractions_for_destination and set it equal to attractions[destination_index], so the way I understand it is that we are dialing in on the sublist within attractions. I get this part too. What I do not get, is the attractions_for_destination.append(attraction). Why are we appending attractions_for_destination which is in the local scope? When you call this function, the attractions in the global scope are modified. Could someone explain?

Dan

print('Hello world!')