Challenge - 5 Problems
Exception Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this code?
Look at the code below. What will it print when run?
Python
try: x = 5 / 0 except ZeroDivisionError: print("Error caught")
Attempts:
2 left
💡 Hint
Think about what happens when you divide by zero in Python.
✗ Incorrect
Dividing by zero raises a ZeroDivisionError. The except block catches it and prints "Error caught".
❓ Predict Output
intermediate2:00remaining
What error does this code raise?
What error will this code produce when run?
Python
my_list = [1, 2, 3] print(my_list[5])
Attempts:
2 left
💡 Hint
Think about what happens when you try to access a list element with an invalid index.
✗ Incorrect
Accessing an index that is out of range in a list raises an IndexError.
❓ Predict Output
advanced2:00remaining
What is the output of this code?
What will this code print?
Python
d = {'a': 1, 'b': 2}
try:
print(d['c'])
except KeyError:
print('Key not found')Attempts:
2 left
💡 Hint
What happens when you try to access a dictionary key that does not exist?
✗ Incorrect
Accessing a missing key in a dictionary raises a KeyError, which is caught and prints 'Key not found'.
❓ Predict Output
advanced2:00remaining
What error does this code raise?
What error will this code produce?
Python
x = 'hello' print(x + 5)
Attempts:
2 left
💡 Hint
Think about adding a string and a number.
✗ Incorrect
You cannot add a string and an integer directly, so Python raises a TypeError.
❓ Predict Output
expert2:00remaining
What is the output of this code?
What will this code print when run?
Python
try: a = int('abc') except ValueError: print('Conversion failed') else: print('Conversion succeeded')
Attempts:
2 left
💡 Hint
What happens when you try to convert a non-numeric string to int?
✗ Incorrect
int('abc') raises a ValueError, so the except block runs and prints 'Conversion failed'.