Boredless Tourist: Help understanding the add_attraction function

#1

Hi there,

I finished the Borderless Tourist, and it works! However, I’m not 100% confident I understand how this function is working. The purpose of it, is to take in a destination and an attraction and add that attraction to the attractions list in the right index, per the destination:

def add_attraction(destination, attraction):
  try:
    destination_index = get_destination_index(destination)
    
    attractions_for_destination = attractions[destination_index]
    
    attractions_for_destination.append(attraction)
    return
    
  except ValueError: print("Destination doesn't exist!")
  return
  • destination_index takes destination as an argument and returns that destinations index in the attractions list–which has an index of 0-4, in which I store destinations per city.
  • attractions_for_destination takes the destinations stored in the attractions list, at the index that was retrieved
  • I then append the attraction that is passed to this add_attraction function to the attractions_for_destination variable, and return.

At what point does the attraction/destination combo get stored into the attractions list?

#2

This exercise uses parallel lists. destinations is a list of strings, and attractions is a list of lists, having the same number of lists as destinations has strings.

There is no “combo” except for the fact that the script is designed so that the indexes of the destinations list always match the indexes of the attractions list. The first expression for both add_attraction() and find_attractions() is get_destination_index(destination)

The whole thing could be much better handled with dictionaries, but they do not arise until the next course.

2 Likes
#3

Thanks Patrickd314 helpful as usual. The below line seems to populate the variable attractions_for_destination with whatever is in the correct index within the attractions list.

attractions_for_destination = attractions[destination_index]

I’m wondering which part of the code actually stores the attraction into the attractions list itself, so that other functions could then call that data.

#4

The next one:
attractions_for_destination.append(attraction)