The Boredless Tourist - Add_attraction function help

https://www.codecademy.com/paths/computer-science/tracks/cspath-cumulative-tourism/modules/cspath-boredless-tourist/projects/the-boredless-tourist

Hello,

What am I doing wrong that when printing attractions it’s clear that the function add_attraction isn’t working as intended. I’ve watched the walkthrough provided and my code looks practically the same except I used list comprehension instead of a written out loop statement to create my attractions list.

My code:

#Test data
test_traveler = ['Erin Wilkes', 'Shanghai, Chaina', ['historical site', 'art']]

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

attractions = [[] for destination in destinations]


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

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

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


add_attraction('Los Angeles', ['Venice Beach', ['beach']])
print(attractions)
#print(attractions)
#test_destination_index = get_traveler_location(test_traveler)
#print(test_destination_index)
#print(get_destination_index('Paris, France'))

Does anyone have an idea? The current terminal output is as follows:

$ python3 script.py
[[], [], [], [], []]

Also, the first time using the .index() function but seems like that section is working fine.
Thanks :slight_smile:

P.S: I’m following the CS path so I tagged the topic as list-functions/general as the Boredless Tourist project comes after the introduction to list, iteration and loops
Edit1: Added Code
Edit2: Forgot to write what the edit was so edited to explain the edit

Have your tried using print statements or similar to see what sections of your code are being executed?

I’d encourage you to use the in operation instead of deliberately throwing errors as a method of testing membership and be careful with .index() as it will only return the first appearance of a match (if your list is full of unique items which this example seems to be then that’s less important).

Tried this last night (printing to see which sections are working. Seems the var “attractions_for_destination” under the scope of the function “add_attractions” isn’t actually running the .append section of the code.

Any idea why?
Maybe I should keep going on with the course and come back to this assignment after the next section.

Have you tried running these functions on their own before? Are you sure aboout what you’re passing and what is being returned. Why not find out exactly what happens on each line.

1 Like

Okay will try out each line :slight_smile:

Thank you

1 Like

Well I’ve gotten it to work.

Updated Code:

#Test data
destinations = ['Paris, France', 'Shanghai, China', 'Los Angeles, USA', 'São Paulo, Brazil', 'Cairo, Egypt']
test_traveler = ['Erin Wilkes', 'Shanghai, China', ['historical site', 'art']]

#Finds the idex for a destination.
def get_destination_index(destination):
  destination_index = destinations.index(destination)
  return destination_index

#Finds the index for the destination of the traveler
def get_traveler_location(traveler):
  traveler_destination = traveler[1]
  traveler_destination_index = get_destination_index(traveler_destination)
  return traveler_destination_index

#This creates the list attractions with sub lists that match the amount of destinations. Meaning if there are five destinations then there will be five sub lists created. 
attractions = [[] for i in destinations]

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

  except ValueError:
    print('add_attractions is throwing a valueError! Oh no :(')
  except:
    print('add+attractions is throwing another error of some kind! Oh no :L')
  




#Tests
#Test get_destination_index
#print(get_destination_index('Paris, France'))
#Test get_traveler_location
#test_destination_index = get_traveler_location(test_traveler)
#print(test_destination_index)
#Tests the list attractions
#print(attractions)
#Tests add_attraction
add_attraction('Los Angeles, USA', ['Venice Beach', ['beach']])
print(attractions)