Programming Assignment Unit 3
Programming Assignment Unit 3
8 of your
textbook
Python Code:
def countdown(n):
if n <= 0:
print('Blastoff!')
else:
print(n)
countdown(n-1)
def countup(n):
if n >= 0:
print('Blastoff!')
else:
print(n)
countup(n+1)
def main():
n = int(input("Enter a number: "))
if n > 0:
countdown(n)
elif n < 0:
countup(n)
else:
print("Blastoff!")
main()
Output:
Output:
3. For Zero:
Python Code:
def countdown(n):
if n <= 0:
print('Blastoff!')
else:
print(n)
countdown(n-1)
def countup(n):
if n >= 0:
print('Blastoff!')
else:
print(n)
countup(n+1)
def main():
n = int(input("Enter a number: "))
if n > 0:
countdown(n)
elif n < 0:
countup(n)
else:
print("Blastoff!")
main()
Output:
Error handling is essential to ensure that programs can recover from unexpected
situations without crashing. In the case of division by zero, attempting to divide any
number by zero causes a ZeroDivisionError in Python, which would crash the
program if unhandled.
By using try-except blocks, we can catch this error and provide a meaningful error
message to the user instead of letting the program crash. This improves user
experience and the program's robustness, especially when handling external input,
which is often unpredictable.
If the division by zero error is not handled, the program will terminate abruptly, and
users may lose confidence in the application’s reliability. In more critical systems,
unhandled errors like this could cause severe consequences, such as data corruption,
incomplete processes, or even security vulnerabilities.