Q.Program 5-11: Example of a program which generates runtime error.
You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.
Start your 14-day free trial to unlock the full solution →A syntactically correct program can still crash while running — dividing by a user-entered 0 raises ZeroDivisionError, the textbook example of a runtime error.
The idea (runtime errors). Errors come in stages. A syntax error is caught before execution — the code never runs. A runtime error occurs during execution: the statement is legal Python, but the operation is impossible for the particular values it meets (dividing by zero, converting "abc" with int(), opening a missing file). The interpreter then stops the program and prints a traceback naming the file, the line and the exception.
Program
# prog5-11.py
# Example of a program which generates a runtime error
num1 = 10.0
num2 = int(input("Enter a number: "))
print(num1 / num2)
Sample run 1 — normal data (user enters 5):
Enter a number: 5
2.0
Sample run 2 — the runtime error (user enters 0):
Enter a number: 0
Traceback (most recent call last):
File "prog5-11.py", line 3, in <module>
print(num1 / num2)
ZeroDivisionError: float division by zero
``` …
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.