The boredless tourist

Boredless tourist

so ive gotten pretty far in this lesson, but at this point i get returned an empty list, can someone point out where i went wrong. thanks.

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

test_traveler = ["erin wilkes", "Shanghai, China", "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):
  try:
    destination_index = get_destination_index(destination)
    attractions_for_destination = attractions[destination_index].append(attraction)
  except SyntaxError:
      return

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("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(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 interests in attraction_tags:
        attractions_with_interest.append(possible_attraction)
  return attractions_with_interest

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

print(la_arts)

You have made a typo here:
if interests in attraction_tags:

:slight_smile:

(Note to self… more caffeine needed. :man_facepalming: )

Thanks Dude

I had a real problem spotting this error.

question, how did you spot it? did you just look over the code, or any tips on how i could do better in the future. Thanks

1 Like

https://asciinema.org/a/MgSec6M52xJT6qhBhFgnpyhAe

1 Like

A lot of this code … cancels out, here’s what I’m left with when removing/changing things that don’t do anything:

from collections import defaultdict


def find_attractions(destination, interests):
    return [
        attraction
        for attraction in attractions[destination]
        for interest in interests
        if interest in attraction[1]
    ]


def add(destination, attraction):
    attractions[destination].append(attraction)


attractions = defaultdict(list)


add("Los Angeles, USA", ["Venice Beach", ["beach"]])
add("Paris, France", ["the Louvre", ["art", "museum"]])
add("Paris, France", ["Arc de Triomphe", ["historical site", "monument"]])
add("Shanghai, China", ["Yu Garden", ["garden", "historcical site"]])
add("Shanghai, China", ["Yuz Museum", ["art", "museum"]])
add(
    "Shanghai, China",
    ["Oriental Pearl Tower", ["skyscraper", "viewing deck"]],
)
add("Los Angeles, USA", ["LACMA", ["art", "museum"]])
add("São Paulo, Brazil", ["São Paulo Zoo", ["zoo"]])
add("São Paulo, Brazil", ["Pátio do Colégio", ["historical site"]])
add("Cairo, Egypt", ["Pyramids of Giza", ["monument", "historical site"]])
add("Cairo, Egypt", ["Egyptian Museum", ["museum"]])


test_traveler = ["erin wilkes", "Shanghai, China", "art"]
print(find_attractions("Los Angeles, USA", ["art"]))

… gets easier to see what’s going on when you prune the unnecessary stuff

… could go a bit further yet and use a finished structure of the data:

attractions = {
    "Cairo, Egypt": [
        ["Pyramids of Giza", ["monument", "historical site"]],
        ["Egyptian Museum", ["museum"]],
    ],
    "Los Angeles, USA": [
        ["Venice Beach", ["beach"]],
        ["LACMA", ["art", "museum"]],
    ],
    "Paris, France": [
        ["the Louvre", ["art", "museum"]],
        ["Arc de Triomphe", ["historical site", "monument"]],
    ],
    "Shanghai, China": [
        ["Yu Garden", ["garden", "historical site"]],
        ["Yuz Museum", ["art", "museum"]],
        ["Oriental Pearl Tower", ["skyscraper", "viewing deck"]],
    ],
    "São Paulo, Brazil": [
        ["São Paulo Zoo", ["zoo"]],
        ["Pátio do Colégio", ["historical site"]],
    ],
}


def find_attractions(destination, interests):
    return [
        attraction
        for attraction in attractions[destination]
        for interest in interests
        if interest in attraction[1]
    ]


print(find_attractions("Los Angeles, USA", ["art"]))

Am I starting to defeat the purpose of the program? Idk, depends on what the purpose of the program is. It’s got the same information in it, and the same features (lookup of attractions)

Maybe it would make more sense if the attractions needed to be read from file. Say, for example that there are destination: attraction pairs… encoded as json, like so:

[
  ["Los Angeles, USA", ["Venice Beach", ["beach"]]],
  ["Paris, France", ["the Louvre", ["art", "museum"]]],
  ["Paris, France", ["Arc de Triomphe", ["historical site", "monument"]]],
  ["Shanghai, China", ["Yu Garden", ["garden", "historcical site"]]],
  ["Shanghai, China", ["Yuz Museum", ["art", "museum"]]],
  ["Shanghai, China", ["Oriental Pearl Tower", ["skyscraper", "viewing deck"]]],
  ["Los Angeles, USA", ["LACMA", ["art", "museum"]]],
  ["São Paulo, Brazil", ["São Paulo Zoo", ["zoo"]]],
  ["São Paulo, Brazil", ["Pátio do Colégio", ["historical site"]]],
  ["Cairo, Egypt", ["Pyramids of Giza", ["monument", "historical site"]]],
  ["Cairo, Egypt", ["Egyptian Museum", ["museum"]]]
]

Then… it might be loaded as:

from collections import defaultdict
import json


def find_attractions(destination, interests):
    return [
        attraction
        for attraction in attractions[destination]
        for interest in interests
        if interest in attraction[1]
    ]


with open("indata.json") as file:
    entries = json.load(file)
attractions = defaultdict(list)
for name, attraction in entries:
    attractions[name].append(attraction)


print(find_attractions("Los Angeles, USA", ["art"]))

@thepitycoder @blog9145668123

HI man I think I’m having the same issue, but im not quite sure what the typo is. I’ve possible spent too much time on this and my brains gone to poo or I’m just never going to get this. Any insights into what I’ve done wrong?

Much appreciated to any help

My code is below:

destinations = ["Paris, France", "Shanghai, China", "Los Angeles, USA", "São 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"))


def get_traveler_location(traveler):
  traveler_destination = traveler[1]
  traveler_destination_index = destinations.index(traveler_destination)
  return traveler_destination_index

test_destination_index = get_traveler_location(test_traveler)
#print(test_destination_index)

attractions = [] 
for destination in destinations:
  attractions.append([])
print(attractions)

def add_attractions(destination, attraction):
  try:
      destination_index = get_destination_index(destination)
      attractions[destination_index].append(attraction)
      return attractions
  except SyntaxError: 
    return 

#print(add_attractions("Los Angeles, USA",['Venice Beach', '["beach"]']))

add_attractions("Paris, France", ["the Louvre", ["art", "museum"]])
add_attractions("Paris, France", ["Arc de Triomphe", ["historical site", "monument"]])
add_attractions("Shanghai, China", ["Yu Garden", ["garden", "historcical site"]])
add_attractions("Shanghai, China", ["Yuz Museum", ["art", "museum"]])
add_attractions("Shanghai, China", ["Oriental Pearl Tower", ["skyscraper", "viewing deck"]])
add_attractions("Los Angeles, USA", ["LACMA", ["art", "museum"]])
add_attractions("São Paulo, Brazil", ["São Paulo Zoo", ["zoo"]])
add_attractions("São Paulo, Brazil", ["Pátio do Colégio", ["historical site"]])
add_attractions("Cairo, Egypt", ["Pyramids of Giza", ["monument", "historical site"]])
add_attractions("Cairo, Egypt", ["Egyptian Museum", ["museum"]])

def find_attractions(destination, interests):
  destination_index = get_destination_index(destination)
  attractions_in_city = attractions[destination_index]
  attractions_with_interests = []
  
  for attraction in attractions_in_city:
    possible_attraction = attraction
    attraction_tag = attractions[1]
    
  for interest in interests:
      if interest in attraction_tag:
        attractions_with_interests.append(possible_attraction)
      else:
        attractions_with_interests.append("no interesting attractions!")
  return attractions_with_interests


print(find_attractions("Los Angeles, USA", 'art'))

You’d begin your search by by considering how it behaves differently from what you expect.
Once you have something measurable you can look at where the offending thing came from, repeat until you’re at the source of the problem.

And since you’re both catching syntax error…
No. that will not happen in that code ever. Nothing in that try block would EVER raise a SyntaxError

So if the code runs anyway, does that mean catching an error was never needed?
It’s either dead code or a bug, either way it shouldn’t be there.

And, maybe that’s saying something about your bugs as well. Are you copying code blindly? That’s not how to write code, if you’re doing that then you’re not programming, you are copying text.

Write code. Run it. Observe the result. Write some more. If you write code to catch an exception, then test it with something that causes that exception to be caught.

Otherwise it’s just sprinkling on magic fairy dust hoping that makes it better. I guarantee that attitude will make it worse.

All the more reason to test things out when following a tutorial, a tutorial is for showing how to get things to happen… so watch them happen.