The Boredless Tourist

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

Hi people. I’m unsure as to why this code is not working. All the other code in the file has been verified to be correct, so there must be something faulty in this particular function.

def find_attractions(destination, interests):
  destination_index = get_destination_index(destination)
  attractions_in_city = attractions[destination_index]
  attractions_with_interest = []
  for i in attractions_in_city:
    possible_attraction = i
    attraction_tags = attractions[1]
    for interest in interests:
      if interest == attraction_tags: 
        attractions_with_interest.append(possible_attraction)
  return attractions_with_interest

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

print(la_arts)

#Desired output in terminal
['LACMA', ['art', 'museum']]

#Actual output in terminal
[]
1 Like
def find_attractions(destination, interests):
  destination_index = get_destination_index(destination)
  attractions_in_city = attractions[destination_index]
  attractions_with_interest = []
  for i in attractions_in_city:
    possible_attraction = i
    attraction_tags = attractions[1]
    for interest in interests:
      if interest == attraction_tags: 
        attractions_with_interest.append(possible_attraction)
  return attractions_with_interest

it is hard to find out where the problem is without all the code since we can’t test the code our self’s. There is however a interesting thing about this function that might explain your results.

The first for loop is a little odd since attractions_in_city should contain only 1 element. Unless this element is a list/array there is no reason to loop over it.

attractions_in_city = attractions[destination_index] // this contains only one element with the index "destination_index"
for i in attractions_in_city:

If this does not explain the results your having. Could you add the rest of the code so we can look at it more closely ?
I am not a pro user myself so i do not have access to the code provided in the link you posted.

1 Like

Hi @biirra Thanks for the response. I painstakingly went through the entire file to see if the logic was correct. It turns out that line 9 was wrong. I adjusted the code to:

if interest in attraction_tags:

This solved the issue. In hindsight, it seems obvious that a single interest would not be equal to all the tags for the places in a city. I wasn’t even aware that in could be used inside an if statement.

1 Like

It’s easier to envision if we consider a while loop…

>>> sequence = [1,2,3,1,4,5,1,6,7,1,8,1,9]
>>> while 1 in sequence:
    sequence.remove(1)

    
>>> sequence
[2, 3, 4, 5, 6, 7, 8, 9]
>>> 
1 Like

Am I correct in that the difference between a while & for loop is that a while loop has an inbuilt if statement?

The argument is a conditional expression. akin to an if.

The in operator tests for membership, the qualifying condition for the loop to proceed.

https://docs.python.org/3/reference/expressions.html#membership-test-details

Found this post on SO that compares if..in with for..in. There links to the documentation of each.

1 Like