Consider the code given below and fill in the blanks.
print("Learning Exceptions...")
try:
num1= int(input ("Enter the first number"))
num2=int(input("Enter the second number"))
quotient=(num1/num2)
print("Both the numbers entered were correct")
except ___________: # to enter only integers
print ("Please enter only numbers")
except __________: # Denominator should not be zero
print("Number 2 should not be zero")
else:
print("Great .. you are a good programmer")
_________: # to be executed at the end
print("JOB OVER... GO GET SOME REST")
Consider the code given below and fill in the blanks.
print("Learning Exceptions...")
try:
num1= int(input ("Enter the first number"))
num2=int(input("Enter the second number"))
quotient=(num1/num2)
print("Both the numbers entered were correct")
except ___________: # to enter only integers
print ("Please enter only numbers")
except __________: # Denominator should not be zero
print("Number 2 should not be zero")
else:
print("Great .. you are a good programmer")
_________: # to be executed at the end
print("JOB OVER... GO GET SOME REST")
Solution
int() raises a ValueError when the input is not a valid integer, such as letters or a decimal string. So the first blank is ValueError.
Dividing by zero raises a ZeroDivisionError, so that fills the second blank.
The last block runs at the end whether or not an exception occurred, which is what finally does. So the third blank is finally.
Option 1: except cannot follow else, and it would not run unconditionally anyway.
Options 3 and 4: NameError is raised for an undefined variable, not for bad input or division by zero.
Related questions:
2025: 27 May Shift 2
2025: 30 May Shift 2