The Boredless Tourist function question(s)

This post if for some questions on the Boredless Tourist python project, link is []here. I started a new thread because I didn’t want to see any extra spoilers yet. This is primarily to see if my cabeza is in the right place.

In step 29 and 30, the instructions call for a try/except block to be added. Steps 31 and 32 call for what happens if the try block is successful. My code as originally written, which kicked out an error:

def add_attraction(destination, attraction):  
    destination_index = get_destination_index(destination)
  except ValueError:
  attractions_for_destination = attractions[destination_index]


I might have been confusing the sequence of instruction for the sequence of the code, but am I correct in understanding that, in a try/except block, everything the function needs to do is placed into the try block? And the except block will essentially end the function. The way I wrote it was basically 1) try to get index, 2) if ValueError, return, 3) if no error, do the next two steps, which python didn’t like.

And the try block here doesn’t require a return attractions_for_destination because it’s appending to a global list?

Secondly, starting at step 38 the instructions call for the find_attractions() function to be created. Step 42 calls for the first for loop, and asks matches to be saved to a temporary variable. Here is what I originally wrote, which didn’t work:

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 = attractions_in_city.append(attraction)
    for pa in posible_attraction:
    attraction_tag = pa[1]

Would ‘local variable’ be a better phrase than ‘temporary variable’ for this? My understanding is that temporary variables are located in the for statement.

And I see now that that code doesn’t work because .append() would create a list of possible attractions, which isn’t what we need. The phrase ‘saving each’ in step 42 of the instructions initially led me to think it was looking for a list.

Hi there. :slight_smile:

No, not quite.

The purpose of try/except is for handling exceptions.

In short, everything which you want to try and do goes inside your try block. If, during the execution of that code, we try and do something which is not allowed - for example, let’s say we try and access a non-existent index in a list - Python will raise an exception.

At this point, when the exception is raised, Python will run your except block. If you have an except block which targets a specific error - like your except ValueError: block - it will run only if you encounter that particular type of error.

I can’t put my finger immediately on the documentation which explains this, but I tried - admittedly with a simplistic example - to recreate that program flow on my machine in Python:

def exceptest(a,b):
        return a/b
        print("Exception block!")

    print("Continued with function!")
    return 0


The output in the console is:

Exception block!
Continued with function!

It would seem that approach ought to work, however…

You might not know, but the try/except structure also permits an else block. If you add an else to this flow structure, it must come after all your except blocks and it will only run if no exceptions are raised from your try block.

So, you could potentially re-work your code:

def add_attraction(destination, attraction):  
        destination_index = get_destination_index(destination)
    except ValueError:
        attractions_for_destination = attractions[destination_index]

Yes, they’ve used “temporary variable” to mean “variable which exists inside this function” - which is a local variable. :slight_smile:

Thank you for your reply, pitycoder. That clears up my question about the try/except blocks. And that bit about adding an else clause to them is very helpful! I’m going to add that in and see how the function works. Cheers.

1 Like