The ZeroDivisionError except block runs, printing "Zero Division Error". The finally block always runs, printing "Done".
Final Answer:
Zero Division Error
Done -> Option A
Quick Check:
ZeroDivisionError caught + finally runs = output A [OK]
Hint: Match error type to except block, finally always runs [OK]
Common Mistakes:
Confusing ValueError with ZeroDivisionError
Forgetting finally block always runs
Expecting else block to run on error
4.
Find the error in this code snippet and choose the correct fix:
try:
x = int('abc')
except ValueError, TypeError:
print("Error occurred")
medium
A. Change except line to: except ValueError | TypeError:
B. Change except line to: except (ValueError, TypeError):
C. Change except line to: except ValueError, TypeError:
D. No change needed, code is correct
Solution
Step 1: Identify syntax error in except line
The syntax except ValueError, TypeError: is invalid for catching multiple exceptions.
Step 2: Correct syntax for multiple exceptions
Use a tuple of exceptions inside parentheses: except (ValueError, TypeError):.
Final Answer:
Change except line to: except (ValueError, TypeError): -> Option B
Quick Check:
Multiple exceptions need parentheses tuple [OK]
Hint: Use parentheses tuple for multiple exceptions in one except [OK]
Common Mistakes:
Using commas without parentheses
Using pipe | operator incorrectly
Assuming original syntax is valid
5.
You want to write a function that tries to convert a string to an integer and then divide 100 by that number. It should handle ValueError if conversion fails, ZeroDivisionError if division by zero happens, and print "Success" if no error occurs, and print "Done" regardless of errors. Which code correctly implements this?
hard
A. def func(s):
try:
n = int(s)
result = 100 / n
except ValueError:
print("Conversion error")
except ZeroDivisionError:
print("Division by zero")
else:
print("Success")
B. def func(s):
try:
n = int(s)
result = 100 / n
except (ValueError, ZeroDivisionError):
print("Error")
finally:
print("Success")
C. def func(s):
try:
n = int(s)
result = 100 / n
except ValueError:
print("Conversion error")
except ZeroDivisionError:
print("Division by zero")
finally:
print("Success")
D. def func(s):
try:
n = int(s)
result = 100 / n
except ValueError:
print("Conversion error")
except ZeroDivisionError:
print("Division by zero")
else:
print("Success")
finally:
print("Done")
Solution
Step 1: Check handling of exceptions and success message
Function must catch ValueError and ZeroDivisionError separately and print appropriate messages.
Step 2: Check use of else and finally blocks
else runs only if no exception, so "Success" should be printed there. finally always runs, so "Done" can be printed there.
Step 3: Verify option correctness
def func(s):
try:
n = int(s)
result = 100 / n
except ValueError:
print("Conversion error")
except ZeroDivisionError:
print("Division by zero")
else:
print("Success")
finally:
print("Done") correctly uses separate except blocks, prints "Success" in else, and "Done" in finally.
Final Answer:
Option D code correctly implements all requirements -> Option D
Quick Check:
Separate except + else for success + finally for done [OK]
Hint: Use else for success, finally for cleanup, separate except blocks [OK]
Common Mistakes:
Printing success in finally instead of else
Combining exceptions in one except without separate messages