0
0
Pythonprogramming~10 mins

Multiple exception handling in Python - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to catch the ValueError exception.

Python
try:
    x = int(input('Enter a number: '))
except [1]:
    print('Invalid input!')
Drag options to blanks, or click blank then click option'
AValueError
BTypeError
CIndexError
DKeyError
Attempts:
3 left
💡 Hint
Common Mistakes
Using TypeError instead of ValueError
Catching unrelated exceptions like IndexError
2fill in blank
medium

Complete the code to catch both ValueError and ZeroDivisionError exceptions in one except block.

Python
try:
    result = 10 / int(input('Enter a number: '))
except [1]:
    print('Invalid input or division error!')
Drag options to blanks, or click blank then click option'
AValueError, ZeroDivisionError
B{ValueError, ZeroDivisionError}
C(ValueError, ZeroDivisionError)
D[ValueError, ZeroDivisionError]
Attempts:
3 left
💡 Hint
Common Mistakes
Using commas without parentheses
Using list or set brackets instead of parentheses
3fill in blank
hard

Fix the error in the except clause to correctly catch ZeroDivisionError and ValueError.

Python
try:
    x = int(input('Enter a number: '))
    y = 10 / x
except [1]:
    print('Error occurred!')
Drag options to blanks, or click blank then click option'
A[ZeroDivisionError, ValueError]
BZeroDivisionError, ValueError
CZeroDivisionError | ValueError
D(ZeroDivisionError, ValueError)
Attempts:
3 left
💡 Hint
Common Mistakes
Writing exceptions separated by commas without parentheses
Using list or set brackets instead of tuple
4fill in blank
hard

Fill both blanks to catch KeyError and IndexError exceptions separately.

Python
try:
    value = my_dict['key']
    item = my_list[10]
except [1]:
    print('Key not found!')
except [2]:
    print('Index out of range!')
Drag options to blanks, or click blank then click option'
AKeyError
BIndexError
CValueError
DTypeError
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up KeyError and IndexError
Using ValueError or TypeError incorrectly
5fill in blank
hard

Fill all three blanks to handle ValueError, ZeroDivisionError, and a generic exception.

Python
try:
    x = int(input('Enter a number: '))
    y = 10 / x
except [1]:
    print('Invalid number!')
except [2]:
    print('Cannot divide by zero!')
except [3]:
    print('Some other error occurred.')
Drag options to blanks, or click blank then click option'
AValueError
BZeroDivisionError
CException
DTypeError
Attempts:
3 left
💡 Hint
Common Mistakes
Not including a generic exception handler
Mixing up exception types