Boredless tourist: attractions_for_destination?


#1

I’ve been attempting to complete this project. I don’t understand the logic behind the function add_attraction(). Inside the function, we need to make a variable attractions_for_destination, append the value passed by attraction variable, and return nothing?

by that logic,

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:  
    return

if I do this, because there is nothing that modifies attractions variable, every time attractions_for_destination = attractions[destination_index] is run, it will assign an empty list, I think? I honestly don’t understand the logic. why not append the attraction directly to the attractions list, like attractions[destination_index].append(attraction), instead of creating a variable attractions_for_destination?

PS: How do I properly add indentation? my whitespace is eaten.


#2

Hi @iamlearningjava

Regarding the indentation, you need to format your posted code. See How to ask good questions (and get good answers) for advice.

The add_attraction function adds an attraction to the attractions object, which is a list of lists. the function just needs to perform that action, and does not need to return anything in particular.

Each of the lists within attractions is a list of attractions for a particular destination. The list of attractions for each destination is located at an index that is the same as the index of that destination within the destinations list.

The add_attraction function uses the get_destination_index function to find the index of destination and saves it to destination_index. Then it uses that same index to access that destination's list of attractions within the attractions list. It saves a reference to that list to attractions_for_destination. Then it uses that attractions_for_destination reference to append the attraction to that destination's list of attractions.

If everything went well, then good, we have added an attraction for destination. If not, perhaps because we had an invalid destination or attraction, we have a ValueError, and nothing got added.