Try block in "The Boredless Tourist"-challenge failing

Hi, I’m doing the “The Boredless Tourist” challenge in the computer science Python class.

At step 29, it asks me to make a try block within a function called add_attraction to prevent a ValueError.
I have written it like this:

def add_attraction(destination, attraction):
    destination_index = get_destination_index(destination)
  except ValueError:

This is also how the try block is written within the hints:

"Use try/catch blocks like this:

    some_thing = can_trigger_a_syntax_errror()
except SyntaxError:  
    print("Error caught!")

If I look at the walkthrough video up to this point, the programmer in the video is writing the try block exactly as I do (except he doesn’t put an indent under try, but when I do that, it says “expected indent”). The programmer in the video never tries his try block, so I don’t get any help from there.

My problem is that the try block should prevent a value error, if I put in a destination that is not in the original “destinations” list. But even with the try block, I get a ValueError returned to me. The ValueError must come from there, because all the code up until that function is tested and works well. Can somebody point me in the right direction here?

EDIT: This is the error I get in the terminal:
" python3 Traceback (most recent call last): File "", line 25, in <module> test_destination_index = get_traveler_location(test_traveler) File "", line 22, in get_traveler_location traveler_destination_index = get_destination_index(traveler_destination) File "", line 12, in get_destination_index destination_index = destinations.index(destination) ValueError: 'Hyderabad, India' is not in list "

Thank you

Could you please post your full code and also the exercise/project url? That would make it a lot easier

Hi, thank you for you reply. URL:

EDIT: Don’t get proper indentation here, look here pls dpaste: 4W5JHM9J5
Here is the whole code:

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

test_traveler = ['Erin Wilkes', 'Shanghai, China', ['historical site', 'art']]

attractions = [[ ] for item in destinations]

def get_destination_index(destination):
  destination_index = destinations.index(destination)
  return destination_index

# print(get_destination_index("Hyderabad, India"))

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)


def add_attraction(destination, attraction):
    destination_index = get_destination_index(destination)
  except ValueError:

you would need to apply format:

[How to] Format code in posts

I don’t get any exception when I run your code:

Try catch allows us to handle exceptions, but this only happens when the exception occurs within the try/catch block. So in your get_traveler_location function where you call get_destination_index:

traveler_destination_index = get_destination_index(traveler_destination)

an exception can still occur.

Sorry, I didn’t realize I had changed the value back in “test_traveler” on row 4.

Please, if you change the value at index 1 in that list “Shanghai, China” to any destination that is not in the “destinations”-list (for example, Hyderabad, India as in the example), it gives the value error.

Okay, but then what I also said in my reply still counts:

print(get_destination_index("Hyderabad, India")) is not in a try-catch block.

Exceptions are useful, stopping the execution of the program when we have something invalid is useful. However, if you want your program to continue running, you will have to handle the exception. Every time. Or you should not raise an exception.

Hmm, I’m struggling to follow here. I’ve been programming for like 4 days, so I don’t “speak code” so to say.

I just see the instructions saying
" What happens if the destination passed in to add_attraction() doesn’t exist? Right now we want to ignore it.

Add a try block to the body of add_attraction() and catch the possible ValueError that could happen when you define destination_index ."

Which is what I’ve done, and what the programmer in the walkthrough video does, and it still doesn’t work.

Do you have the possibility to “dumb down” you explanation even more, or am I still missing it at kindergarden level? :stuck_out_tongue:

And if we now add an attraction:

def add_attraction(destination, attraction):
    destination_index = get_destination_index(destination)
  except ValueError:
    print('handle exception')

add_attraction("abc", "def")

we see the result of print('handle exception'), so the exception is being thrown within the get_destination_index function (by .index() method), which is then handled. No attractions are added, but are program also doesn’t crash.

in your code, you never tested adding an attraction