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):
try:
destination_index = get_destination_index(destination)
except ValueError:
return
This is also how the try block is written within the hints:
"Use try/catch blocks like this:
try:
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 script.py
Traceback (most recent call last):
File "script.py", line 25, in <module>
test_destination_index = get_traveler_location(test_traveler)
File "script.py", line 22, in get_traveler_location
traveler_destination_index = get_destination_index(traveler_destination)
File "script.py", line 12, in get_destination_index
destination_index = destinations.index(destination)
ValueError: 'Hyderabad, India' is not in list
"
Thank you