Bordeless Tourist Challenge

Hi,

I keep getting the wrong output for step 48 in the borderless tourist challenge, and I’m not quite sure what I’m doing wrong. So, here’s my code for the part that keeps printing out incorrectly:

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'])
print(la_arts)

And here’s the result I get:

[[['the Louvre', ['art', 'museum']], ['Arc de Triomphe', ['historical site', 'monument']]], [['Yu Garden', ['garden', 'historcical site']], ['Yuz Museum', ['art', 'museum']], ['Oriental Pearl Tower', ['skyscraper', 'viewing deck']]], [['Venice Beach', ['beach']], ['LACMA', ['art', 'museum']]], [['São Paulo Zoo', ['zoo']], ['Pátio do Colégio', ['historical site']]], [['Pyramids of Giza', ['monument', 'historical site']], ['Egyptian Museum', ['museum']]]]
[['LACMA', ['art', 'museum']]]

It does technically give the expected result at the end [‘LACMA’, [‘art’, ‘museum’]], but for whatever reason, it just keeps printing out the entire loop as well. I’ve tried going through every indentation, and a whole host of other things, I just can’t quite work out what I’ve done wrong. What else should I look out for when debugging?
Thanks.

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

Well, logically speaking there is another print statement somewhere in the code.
If it’s not invoked, and you’re sure it’s in this function, that means you should look in side your function for any other function calls (as the print call might be inside those functions).

1 Like

Ah, thanks! I ended up finding the issue in the end. I did have a print inside an earlier function.

i also ran into some difficulty here. in my case its doesnt print the correct result but venice beach

im not getting LAXCMA for some reason

You should try placing print statements to track your data. Sometimes it helps to map it out on paper to make sure you’re expected vision is clear and see where it diverges.

thans, still havent figured it out. it returns an empty cell after the 5 from the list that were expected

Can you share you whole code? It’s a bit easier to work with than a screenshot. (for formatting: [How to] Format code in posts)

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

test_traveller = ['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"))

def get_traveller_location(traveller):
  traveller_destination = traveller[1]
  traveller_destination_index = get_destination_index(traveller_destination)
  return traveller_destination_index

test_destination_index = get_traveller_location(test_traveller)
print(test_destination_index)
attractions = []
for destination in destinations:
  attractions.append([])
#comment: either one is good
attractions = [[] for destination in destinations]
print(attractions)

def add_attraction(destination, attraction):
  try:
   destinations_index = get_destination_index(destination)
   attractions_for_destination = attractions[destinations_index].append(attraction)
  except SyntaxError:
   return

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"]])

def find_attractions(destinations, 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"])

print(la_arts)

thank you for your time and effort, done

Ok, so I’d say careful with indentation, honestly I think either a 3-space tab or 4 explicit spaces is the way to go (as 2 are hard to read), but it’s a matter of taste if you’re not working in a team.

I want to highlight that your return statement in find_attractions() is still in your for-loop, which usually is not what you want (unless it’s nested in some if-statement, but that’s for another type of aim).

Then, once I corrected the indent, there’s still no return. The first place I look is the clause that populates the list (which is your for-loop and conditional).

— My rule of thumb is to never trust conditionals to run properly… which means they’re a prime place to look to debug (this is for many reasons, but one being that since python doesn’t have type declarations it’s easier for mistakes to go undetected at the conditional).

So for example, I’d start debugging like this, and go down the rabbit hole (notice i also changed to 3-space indentation for clarity of nesting:

def find_attractions(destinations, 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:
         print(interest in attraction_tags)
         if interest in attraction_tags:
            attractions_with_interest.append(possible_attraction)

   return attractions_with_interest

(notice the condition is returning false, which is why your end result is not what you want, you’d have to investigate deeper as to why that is)
Debugging is perhaps one of the more vital tools for programming so I’ll let you keep trying to untangle this, I just loosened up the knot a little.

thank you again, I will keep at it