Python 3: The Boredless Tourist Project

Hello, I have been doing The Boredless Tourist Project. I am kindly asking for help on my error. I have already followed the video on Get Stuck section but my output is not the expected output. I have already tried to debug my code by using print on every possible way to know the values that gets passed and I noticed my if statement is not running. If I put an else, it will be the one that gets run and I dont know why. Any help would be greatly appreciated

This is my code:

destinations = ["Paris, France", "Shanghai, China", "Los Angeles, USA", "Sao Paulo, Brazil", "Cairo, Egypt"]

test_traveler = ['Erin Wilkes', 'Cairo, Egypt', ['historical site', 'art']]

def get_destination_index(destinate):

  destination_index = destinations.index(destinate)

  return destination_index 

p_dest = get_destination_index("Cairo, Egypt")

#print(p_dest)

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 i in destinations:

  attractions.append([])

#print(attractions)

def add_attraction(destinate, attract):

  try:

    destination_index = get_destination_index(destinate)

    attractions_for_destination = attractions[destination_index].append(attract)

  except ValueError:

    return

add_attraction("Los Angeles, USA", ['Venice Beach', ['beach']])

#print(attractions)

add_attraction("Paris, France", ["the Louvre", ["art", "museum"]])

add_attraction("Paris, France", ["Arc de Triomphe", ["historical site", "monument"]])

add_attraction("Shanghai, China", ["Yu Garden", ["garden", "historcical site"]])

add_attraction("Shanghai, China", ["Yuz Museum", ["art", "museum"]])

add_attraction("Shanghai, China", ["Oriental Pearl Tower", ["skyscraper", "viewing deck"]])

add_attraction("Los Angeles, USA", ["LACMA", ["art", "museum"]])

add_attraction("São Paulo, Brazil", ["São Paulo Zoo", ["zoo"]])

add_attraction("São Paulo, Brazil", ["Pátio do Colégio", ["historical site"]])

add_attraction("Cairo, Egypt", ["Pyramids of Giza", ["monument", "historical site"]])

add_attraction("Cairo, Egypt", ["Egyptian Museum", ["museum"]])

#print(attractions)

def find_attractions(destinate, interest):

  destination_index = get_destination_index(destinate)

  attractions_in_city = attractions[destination_index]

  attractions_with_interest = []

  #print(destination_index)

  #print(attractions_in_city)

  for i in attractions_in_city:

    possible_attractions = i

    attraction_tags = attractions[1]

    #print(possible_attractions)

    #print(attraction_tags)

    for x in interest:

      if x in attractions:

        attractions_with_interest.append(possible_attractions)

      

  return attractions_with_interest

la_arts = find_attractions("Los Angeles, USA", ['art'])

print(la_arts)

This is the output:
[ ]

Expected output is this:
['LACMA', ['art', 'museum']]

I apologize in advance if I tag lens-slice, I cannot find a tag for this project and this is my first time posting here.

The output becomes an empty list.

Welcome to the forums @arlylizano7762180845!

Firstly, what is the purpose of this line: attraction_tags = attractions[1]?


   for x in interest:

     if x in attractions: 

       attractions_with_interest.append(possible_attractions)

Secondly, I assume your line if x in attractions: is supposed to check if the elements in the interest parameter are inside the list of tags associated with each attraction. If so, why are you checking the entire list?

The first iteration of for i in attractions_in_city: gives you ['Venice Beach', ['beach']]. If we would like to check if an element in the interest parameter is a tag in the list of tags belonging to Venice Beach, we would need to access that list (['beach']).

In that attraction list ['beach'] would be the last element or the element at index 1. So really, if x in attractions: needs to be changed to if x in possible_attractions[-1].

So now for every possible_attraction in attractions_in_city:, we are checking the list of tags for the elements in interest. This will return your expected output.

def find_attractions(destinate, interest):

  destination_index = get_destination_index(destinate)

  attractions_in_city = attractions[destination_index]

  attractions_with_interest = []

  #print(destination_index)

  #print(attractions_in_city)

  for i in attractions_in_city:

    

    possible_attractions = i

    

    print(possible_attractions)

    for x in interest:

        if x in possible_attractions[-1]:

            attractions_with_interest.append(possible_attractions)

      

  return attractions_with_interest

la_arts = find_attractions("Los Angeles, USA", ['art'])

print(la_arts)#[['LACMA', ['art', 'museum']]]
1 Like

Now that you say it, I also wonder what the attraction_tags are for. I did it for it was step 43. Maybe it will be used in the next steps.

43. For each attraction, retrieve the tagged information about it. The tags are all saved in the second place (index 1 ) in the attraction. In the body of the for loop, save the attraction’s tags into the variable attraction_tags .

For the if x in attractions: part, my guess is the creator of the project exercise wanted the project to be dynamic enough for the learners like me. Instead of just declaring the element in index -1. That’s only based on my understanding.

But I got your logic, after thinking about how you come up with your answer for the whole day, i finally understand it. Thank you so much for your help! :heart: :heart: :heart:

1 Like

Actually I suspect attraction_tags = attractions[1] is meant to access that list I was talking about.

Notice here how I said that list would be the last element or the element at index 1? It’s possible that is what attractions[1] mean.

However,

It is for each attraction, so you would still need to access i or possible_attractions.

Alternately,

def find_attractions(destinate, interest):

  destination_index = get_destination_index(destinate)

  attractions_in_city = attractions[destination_index]

  attractions_with_interest = []

  #print(destination_index)

  #print(attractions_in_city)

  for i in attractions_in_city:

   
    possible_attractions = i
    attraction_tags = possible_attractions[1]

    #print(possible_attractions)

    for x in interest:

        if x in attraction_tags:

            attractions_with_interest.append(possible_attractions)

      

  return attractions_with_interest

la_arts = find_attractions("Los Angeles, USA", ['art'])

print(la_arts)#[['LACMA', ['art', 'museum']]]