Complete the code to catch a division by zero error.
try: result = 10 / 0 except [1]: print("Cannot divide by zero!")
The ZeroDivisionError is raised when you try to divide a number by zero.
Complete the code to catch an error when accessing a list index that does not exist.
my_list = [1, 2, 3] try: print(my_list[5]) except [1]: print("Index out of range!")
IndexError occurs when you try to access an index that is not in the list.
Fix the error in the code by catching the correct exception type when converting a string to an integer.
user_input = "abc" try: number = int(user_input) except [1]: print("Invalid number!")
ValueError is raised when conversion from string to int fails due to invalid format.
Fill both blanks to catch errors when opening a file that does not exist and when reading from it.
try: with open('missing.txt', '[1]') as file: content = file.read() except [2]: print("File error occurred.")
Opening a file for reading uses mode 'r'. If the file is missing, FileNotFoundError is raised.
Fill all three blanks to create a dictionary comprehension that includes only keys with string values longer than 3 characters.
data = {'a': 'cat', 'b': 'elephant', 'c': 42}
result = {k: v for k, v in data.items() if isinstance(v, [1]) and len(v) [2] [3]We check if the value is a string with isinstance(v, str), then check if its length is greater than 3.