The Boredless Tourist: how test code affect passing arguments into functions

Hi there,

I completed the Boredless Tourist, and it works! However, to get it to print the right attraction in the end, I had to comment out a line of test code (bolded below).

If I leave la_arts alone, allowing it to call find_attractions with the LA arguments, then those LA values will print in the end. Even though the get_attractions_for_travelers function also calls the find_attractions function, and passes different values for the arguments to that function.

I would assume that even if I didn’t comment out the la_arts variable, find_attractions would pass two results–one for la_arts–which I’m not printing, but if I did would show the LA result. And a different result for smills_france, which calls get_attractions_for_travelers, which in turn calls find_attractions–with different argument values.

Why does la-arts supersede smills_france?

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

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

#function connecting people with attractions they're interested in
def get_attractions_for_traveler(traveler):
  traveler_destination = traveler[1]
  traveler_interests = traveler[2]
  rec = []
  interests_string = "Hi " + traveler[0] + ", we think you'll like these places around " + traveler_destination+": " 
  
  **traveler_attractions = find_attractions(traveler_destination, traveler_interests)**
  
  for attraction in traveler_attractions:
    rec.append(attraction)
    joins = attraction 
    
    return interests_string + str(joins)

smills_france = get_attractions_for_traveler(['Dereck Smill', 'Paris, France', ['monument']])

print(smills_france)

When I run your code (with the asterisks removed), it throws an error because the list attractions_with_interest is nowhere defined. Once that is taken care of, it runs fine with or without the la_arts assignment statement. The LA values do not show up; the appropriate Paris values do.

If I add a final line, print(la_arts), then below Dereck’s information, [‘LACMA’] is printed.

1 Like

Thanks Patrickd314! I actually defined attractions_with_interest at the top as a global list, with other lists, and didn’t include that part of the code. Below is my full code–with la_arts uncommented. When I run it, it returns LACMA.

destinations = ["Paris, France", "Shanghai, China", "Los Angeles, USA" , "So Paulo, Brazil",   "Cairo, Egypt"]
attractions = [[], [], [], [], []]
test_traveler = ['Erin Wilkes', 'Shanghai, China', ['historical site', 'art']]
attractions_with_interest = []

#function to get destination index
def get_destination_index(destination):
  destination_index = destinations.index(destination)
  return destination_index

#function to get traveler's location
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)

#function to add attractions per destination - not 100% on how this function is working
def add_attraction(destination, attraction):
  try:
    destination_index = get_destination_index(destination)
    
    attractions_for_destination = attractions[destination_index]
    
    attractions_for_destination.append(attraction)
    return
    
  except ValueError: print("Destination doesn't exist!")
  return
  #except: print("Something went wrong!")
  #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("So Paulo, Brazil", ["So Paulo Zoo", ["zoo"]])
add_attraction("So Paulo, Brazil", ["Ptio do Colgio", ["historical site"]])
add_attraction("Cairo, Egypt", ["Pyramids of Giza", ["monument", "historical site"]])
add_attraction("Cairo, Egypt", ["Egyptian Museum", ["museum"]])

#function to match interests with attractions
def find_attractions(destination, interests):
  destination_index = get_destination_index(destination)
  attractions_in_city = attractions[destination_index]
  
  for possible_attraction in attractions_in_city:
    possible_attraction.append(possible_attraction)
    attraction_tags = possible_attraction[1]
    
    #print(possible_attraction)
    
    for interest in interests:
      for attraction in attraction_tags:
      	if interest == attraction:
          attractions_with_interest.append(possible_attraction[0])
      #print(interest)
      #print(attraction_tags)
      #print(attractions_with_interest)
 
  
  return attractions_with_interest

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

#function connecting people with attractions they're interested in
def get_attractions_for_traveler(traveler):
  traveler_destination = traveler[1]
  traveler_interests = traveler[2]
  rec = []
  interests_string = "Hi " + traveler[0] + ", we think you'll like these places around " + traveler_destination+": " 
  
  traveler_attractions = find_attractions(traveler_destination, traveler_interests)
  
  for attraction in traveler_attractions:
    rec.append(attraction)
    joins = attraction 
    
    
    
    return interests_string + str(joins)

smills_france = get_attractions_for_traveler(['Dereck Smill', 'Paris, France', ['monument']])

print(smills_france)

You just identified the problem! the list, attractions_with_interest should be local to find_attractions(). Otherwise, you lengthen it with every call to find_attractions(), but then (I asked) why doesn’t get_attractions_for_traveler() return every attraction in that global list???

It’s the final for loop:

  for attraction in traveler_attractions:
    rec.append(attraction)
    joins = attraction    
    return interests_string + str(joins)

The way it’s written, it will return only the first attraction in the list, attractions_with_interest, since return halts function execution. And, if you out-dent return so that it is beneath for, it will return the last attraction in traveler_attractions. So that needs a bit of work. I think you might have been thinking along those lines when you brought in that list rec, which you don’t do anything with.


NOTE: A better test for this script than Dereck Small is the test_traveler, Erin Wilkes, who has two areas of interest, ‘historical site’ and ‘art’

I expected Erin to return, Hi Erin Wilkes, we think you'll like these places around Shanghai, China: Yu Garden,Yuz Museum

… but it would not; it returned only the Yuz Museum. More than one hour later, I noticed that, in the add_attraction list provided by the course, we find add_attraction("Shanghai, China", ["Yu Garden", ["garden", "historcical site"]])

Note the spelling of “historical”!!! (Yes, I reported it.)

Thanks a lot patrickd314!

1.) I’ve localized the list attractions_with_interest, I get the rationale that as a global list, it’d consume all content each time the function is called.
2.) I updated that last for loop, significantly simplifying it to just append attraction to interests_string directly

  for attraction in traveler_attractions:
    interests_string = interests_string + ", " + attraction
    
  return interests_string
    

3.) The test_traveler test successfully returns two results. The only defect left with my solution is that there will always be a hanging “,” at the end of the output.

In case you’re curious, the full code:

destinations = ["Paris, France", "Shanghai, China", "Los Angeles, USA" , "So Paulo, Brazil",   "Cairo, Egypt"]
attractions = [[], [], [], [], []]
test_traveler = ['Erin Wilkes', 'Shanghai, China', ['historical site', 'art']]


#function to get destination index
def get_destination_index(destination):
  destination_index = destinations.index(destination)
  return destination_index

#function to get traveler's location
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)

#function to add attractions per destination - not 100% on how this function is working
def add_attraction(destination, attraction):
  
  try:
    destination_index = get_destination_index(destination)
    
    attractions_for_destination = attractions[destination_index]
    
    attractions_for_destination.append(attraction)
    return
    
  except ValueError: print("Destination doesn't exist!")
  return
  #except: print("Something went wrong!")
  #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", "historical 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("So Paulo, Brazil", ["So Paulo Zoo", ["zoo"]])
add_attraction("So Paulo, Brazil", ["Ptio do Colgio", ["historical site"]])
add_attraction("Cairo, Egypt", ["Pyramids of Giza", ["monument", "historical site"]])
add_attraction("Cairo, Egypt", ["Egyptian Museum", ["museum"]])

#function to match interests with attractions
def find_attractions(destination, interests):
  attractions_with_interest = []
  destination_index = get_destination_index(destination)
  attractions_in_city = attractions[destination_index]
  
  for possible_attraction in attractions_in_city:
    possible_attraction.append(possible_attraction)
    attraction_tags = possible_attraction[1]
    
    #print(possible_attraction)
    
    for interest in interests:
      for attraction in attraction_tags:
      	if interest == attraction:
          attractions_with_interest.append(possible_attraction[0])
      #print(interest)
      #print(attraction_tags)
      #print(attractions_with_interest)
 
  
  return attractions_with_interest

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

#function connecting people with attractions they're interested in
def get_attractions_for_traveler(traveler):
  traveler_destination = traveler[1]
  traveler_interests = traveler[2]
  
  interests_string = "Hi " + traveler[0] + ", we think you'll like these places around " + traveler_destination+": "
  
  traveler_attractions = find_attractions(traveler_destination, traveler_interests)
  
  for attraction in traveler_attractions:
    interests_string = interests_string + ", " + attraction
    
  return interests_string

smills_france = get_attractions_for_traveler(['Erin Wilkes', 'Shanghai, China', ['historical site', 'art']])

print(smills_france)

      

1 Like

Try:

  for attraction in traveler_attractions:
    interests_string = interests_string + attraction + ", "       # quote-comma-space-quote
    
  return interests_string.rstrip(" ,")            # quote-space-comma-quote
1 Like