The Boredless Tourist Please Help

Hi everyone,
First time posting

I am doing The Boredless Tourist project and my code has been working well until no.34
https://www.codecademy.com/paths/computer-science/tracks/cspath-cumulative-tourism/modules/cspath-boredless-tourist/projects/the-boredless-tourist

I’ve back tracked through the code, watched the walkthrough multiple times, looked for an explanation on posts,
The problem I can’t solve is adding the attractions to the correct list no.
It’s supposed to be added to the index [2] but keeps being added to index [4]

here is the 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("Los Angeles, USA"))
#print(get_destination_index("Paris, France"))
#print(get_destination_index("Hyderadad, India"))
#print(get_destination_index("Shanghai, China"))
#print(get_destination_index("Cairo, Egypt"))

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(destionation, attraction):
  destination_index = get_destination_index(destination)
  attractions_for_destination = attractions[destination_index].append(attraction)
  return
add_attraction("Los Angeles, USA", ['Venice Beach', ['beach']])

print(attractions)

thanks

# You wrote:
def add_attraction(destionation, attraction):

# It should be:
def add_attraction(destination, attraction):

Since you had a typo in the parameter destionation, so in the add_attraction function, when you make the call

destination_index = get_destination_index(destination)

then destination is not defined in the scope of the function. So, the interpreter goes in the global scope to see whether destination is defined in that scope. In the global scope, you have:

for destination in destinations:

When that loop finishes, destination has the value of the last element of destinations.
In the add_attraction function, the call to get_destination_index is made with this last value instead of the argument you passed to the function which is assigned to the misspelled parameter destionation.

Hi,
Thank you for this solution with the explanation.
My next question would have been why no error is given.

1 Like