Identify the error(s) in the code:
# Code snippet
numbers = [1, 2, 3, 4]
print(numbers[4])
Index Error: The list numbers has indices from 0 to 3, so accessing numbers[4] will raise an IndexError.
What will the following code output:
Input:
6.4
3.2
Code:
try:
value1 = float(input("Enter a floating point number: "))
print(value1 / 2)
value2 = float(input("Enter another floating point number: "))
print(10 / value2)
except:
print('Error occurred')
print('End of program')
3.2
3.125
End of Program
A block of code that utilizes exception handling
What is a try block?
Identify the error(s) in the code:
# Code snippet
def divide(a, b):
return a / b
result = divide(10, 0)
print(result)
ZeroDivisionError: Dividing by zero in the divide function will raise a ZeroDivisionError.
What will the following code output:
Input:
not_a_number
3.2
code:
try:
value1 = float(input("Enter a floating point number: "))
print(value1 / 2)
value2 = float(input("Enter another floating point number: "))
print(10 / value2)
except:
print('Error occurred')
print('End of program')
Error occurred
End of program
True or False:
If no exception occurs, the code will skip over the except and finally clauses and continue with the rest of the code
False:
If no exception occurs, the except clause WILL BE SKIPPED while the finally clause WILL NOT BE SKIPPED.
Identify the error(s) in the code:
# Code snippet
my_dict = {'name': 'Alice', 'age': 25}
print(my_dict['gender'])
KeyError: The key 'gender' does not exist in my_dict, so accessing it will raise a KeyError.
What will the following code output:
Input:
1
6
eight
0
end
code:
user_input = input()
while user_input != 'end':
try:
divisor = int(user_input)
print(60 // divisor)
except ValueError:
print('v')
except ZeroDivisionError:
print('z')
user_input = input()
print('OK')
60
10
v
z
OK
What will the following code output:
function call: divide(9,3)
def divide(a, b):
z = -7
try:
z = a / b
except ZeroDivisionError:
print('Can't divide by 0')
finally:
print(f'Z is equal to {z}')
Output:
Z is equal to 3.0
Identify the error(s) in the code:
# Code snippet
data = [10, 20, 30]
index = int('three')
print(data[index])
ValueError: Converting the string 'three' to an integer with int('three') will raise a ValueError.
IndexError: If the string 'three' were a valid integer conversion but out of range, it would cause an IndexError. However, in this specific case, only the ValueError applies.
What will the following code output:
Input:
-10, 90, 4, seven,-2,0,end
code:
numbers = [2, 4, 5, 8]
user_input = input()
while user_input != 'end':
try:
divisor = int(user_input)
if divisor > 20:
result = compute(result)
elif divisor < 0:
result = numbers[divisor]
else:
result = 20 // divisor
print(result, end=' ')
except (ValueError, ZeroDivisionError):
print('r', end=' ')
except (NameError, IndexError):
print('s', end=' ')
user_input = input()
print('OK')
output:
s s 5 r 5 r OK
What will the following code output:
Input:
50
Code:
try:
eaten_chips = int(input())
if eaten_chips < 0:
raise ValueError('Kevin was seen eating chips')
left_over_chips = 250 - eaten_chips
print(f'Left over chips: {left_over_chips}')
except ValueError as excpt:
print(f'Error: {excpt}')
output:
Left over chips: 200
Identify the error(s) in the code:
# Code snippet
data = {'name': 'Alice', 'scores': [85, 90, 78]}
try:
average_score = sum(data['scores']) / len(data['score'])
print("Average score:", average_score)
last_score = data['scores'][3]
print("Last score:", last_score)
except Exception as e:
print(e)
KeyError: The key 'score' is incorrect. It should be 'scores' in len(data['score']).
IndexError: The list data['scores'] has indices 0, 1, and 2. Accessing data['scores'][3] will raise an IndexError.
What will the following code output:
Input:
four,0,4,-8,zero, 5
code:
user_input = input()
while user_input != 'end':
try:
divisor = int(user_input)
if divisor < 0:
print(compute(divisor), end=' ')
else:
print(20 // divisor, end=' ')
except ValueError:
print('v', end=' ')
except ZeroDivisionError:
print('z', end=' ')
except:
print('x', end=' ')
user_input = input()
print('OK')
output:
v z 5 x v 4 OK
What will the following code output:
Input:
bluesky
greengrass
valid_user_name = False
while valid_user_name == False:
try:
password = input()
if len(valid_user_name) < 10:
raise ValueError('Invalid')
valid_user_name = True
print('Good Name')
except ValueError as excpt:
print(f'Error: {excpt}')
Output:
Error: Invalid
Good Name