I was doing the python project: The Boredless Tourist. I am stuck at task 32 and 33.
Task 32 says, append the attraction
passed into add_attraction
to the list attractions_for_destination
.
What I don’t understand is how is attractions
getting modified here, because we created a variable attractions_for_destination
inside the function add_attraction()
and assigned it to the corresponding list of attraction
in attractions
. Then we appended the attraction passed inside the function to the variable attractions_for_destination
. Nowhere did we modify the lists inside attractions
. But when we call the function in task 33 and then print attractions
, the attractions passed inside the function add_attractions()
has been added to the actual list attractions
.
Here is my code:
destinations = ["Paris, France", "Shanghai, China", "Los Angeles, USA", "Sao 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("Hyderabad, India"))
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 = [[], [], [], [], []]
# print(attractions)
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
add_attraction('Los Angeles, USA', ['Venice Beach', ['beach']])
print(attractions)
Here is the output:
$ python3 script.py
[[], [], [['Venice Beach', ['beach']]], [], []]
$
(Sorry I’m not allowed to add another image)
Please clear this confusion. So much of attraction here!!