The Boredless Tourist

I am doing The Boredless Tourist project and my code has been working well until this part

  def find_attractions(destination, interests):
  destination_index = get_destination_index(destination)
  attractions_in_city =attractions[destination_index]
  attractions_with_interest = []
  for attraction in attractions_in_city:
    possible_attractions = [attraction]
    attraction_tags = attraction[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)

it just prints out empty list :
I think the problem is that in guidelines it just says loop through interests but the problem is interests doesn’t exist.
I don’t know if this is my fault or mistake in guidelines.
Link for the project https://www.codecademy.com/paths/computer-science/tracks/cspath-cumulative-tourism/modules/cspath-boredless-tourist/projects/the-boredless-tourist

You have misspelled either the variable declarion name or the argument in the append method. :slight_smile:

possible_attractions = [attraction]
attractions_with_interest.append(possible_attraction)

Oh and about your theory, interests are declared as an argument of the function in the beginning:
def find_attractions(destination, interests):
and then passed on when the function is called:
la_arts = find_attractions("Los Angeles,USA", "art")
in this case it’s the same as if it said interests = 'art'

2 Likes

thx for reply,I now get it what you saying about interests but I fixed misspelling in possible_attraction and its still the same.
I don’t get it why there wasn’t an error when I misspelled.

Hi @tin1

A quick point to start with - it’s helpful to provide the entirety of your code, not just the problematic function. In this case, I suspect the issue is in the code you’ve provided but the issue is clearer when the whole program is present. [1]

As I understand the project for Boredless Tourist, you’re trying to match a prospective traveler with destinations they might like.

I believe that somewhere in your code, you ought to have a function to create new attractions for a specific destination. The attractions are defined as follows:

attraction = ['Name of destination',['list','of','interest','tags',]]

So, let’s say you have the following attraction on file for Los Angeles (i.e. this is the value held in attractions_in_city:

['Los Angeles County Museum of Art', ['art','museum','farm to table']]

We’ll enter your first for loop, and we’ll get the attraction tags, so attraction_tags = ['art','museum','farm to table']. We’ll then enter the second for loop, where we loop through the travelers interests.

In the case of find_attractions('Los Angeles, USA', 'art') we have only one interest: art. So, we get to:
if interest == attraction_tags:

This is only ever going to evaluate to True here if interest = ['art','museum','farm to table'], because as we’ve already covered attraction_tags = ['art','museum','farm to table'].

Don’t you think it would make more sense here to check whether the current interest you’re checking is in the tags of the attraction we’re considering? :wink:

Edit:

Because that block of code is unlikely to ever be executed, as I have explained. :slight_smile:

[1] This is especially true when we’re dealing with Pro content. Many of the people on the forums, including me, are not Pro users - so I can’t see the project material for Boredless Tourist. Helpfully, I’ve seen the questions about this project enough that I have a basic grasp of it regardless. :slight_smile:

2 Likes

I don’t know where to check if interest is in tags because this is the first time interests are mentioned.
This 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


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)


attractions = [[] for destination in destinations]


def add_attraction(destination, attraction):
  destination_index = get_destination_index(destination)
  try:
    destination_index = get_destination_index(destination)
  except ValueError:
    print("Error caught!")
    return
  attractions_for_destination = attractions[destination_index]
  attractions_for_destination.append(attraction)
  
add_attraction("Los Angeles,USA", ["Venice Beach", ["beach"]])
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("Sao Paulo,Brazil", ["São Paulo Zoo", ["zoo"]])
add_attraction("Sao 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"]])

def find_attractions(destination, interests):
  destination_index = get_destination_index(destination)
  attractions_in_city =attractions[destination_index]
  attractions_with_interest = []
  for attraction in attractions_in_city:
    possible_attraction = [attraction]
    attraction_tags = attraction[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)

edit:
Actually I get it but Los Angeles has art tag and it still doesn’t work

1 Like

I pointed out, in my previous post, precisely where your error is and how to fix it.

Everything you need to fix your very minor error is in that quote. I strongly suggest that you re-read what I originally wrote, carefully, and try and spot the error yourself.

If you wish, however, the correction I am hinting about is here.

If you change if interest == attraction_tags: to if interest in attraction_tags: then you’ll match a single interest from the list of tags.

You will, however, run into another issue - which is that you’re not passing in a list of interests to find_attractions()

Think about what happens if we pass a string as our iterable in a for loop:

for char in "codecademy":
    print(char)

# result:
c
o
d
e
c
a
d
e
m
y

Now, in your call la_arts = find_attractions("Los Angeles,USA", "art") - the iterable here is "art". Can you see how this is wrong? :slight_smile:

Thank you so much for your replies but I just can’t get it right.
This is my code now:

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

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

now I am getting: SyntaxError: unexpected EOF while parsing

1 Like

You’re missing a closing bracket ) in find_attractions("Los Angeles,USA", ["art"] :slight_smile:

hahaha yess I was just so frustrated I didn’t pay attention.
Thank you sooooo much its finally working!

1 Like

:slight_smile: It’s easy to make those kinds of typos when we’re coding in a rush.

No problem, glad I could help. :+1: If you get stuck with anything else, you know where we are. :slight_smile:

How do I fix this new issue of not passing a list to find_attractions()?

One way to fix it would be to test whether the argument for interests is a list inside your function.

If it is already a list, your code is fine. If it’s not, and it’s a string for example, you could convert it to a list.

Can you think of a way to implement an if ... else block which accomplishes that in your function?

(I’ll give you a hint: you can use the isinstance() function to test whether a variable is a specific type.)

1 Like