Python Code for Q1
def countdown(n):
1
if n<= 0:
2
print('Blastoff!')
3
else:
4
print(n)
5
countdown(n-1)
6
7
def countup(n):
8
if n>= 0:
9
print('Blastoff!')
10
else:
11
print(n)
12
countup(n+1)
13
14
# Main Program
15
entry = input("Please enter a number: ")
16
number = int(entry)
17
if number > 0:
18
countdown(number)
19
elif number < 0:
20
countup(number)
21
else:
22
countdown(number)
23
24
Input: -3 (Negative Number)
25
26
= RESTART: C:/Users/gbeng/AppData/Local/Programs/Python/Python313/python
27
homework [Link]
28
Please enter a number: -3
29
-3
30
-2
31
-1
32
Blastoff!
33
34
Input: 3 (Positive Number)
35
======================================= RESTART:
36
C:/Users/gbeng/AppData/Local/Programs/Python/Python313/python homework [Link]
37
======================================
38
Please enter a number: 3
39
3
40
2
41
1
42
Blastoff!
43
44
======================================= RESTART:
45
C:/Users/gbeng/AppData/Local/Programs/Python/Python313/python homework [Link]
46
======================================
Please enter a number: 0
Blastoff!
Explanation of Choice for Zero Input
In my program, I choose to call the countdown method when the user inputs zero. Since zero is
the "base case" or ending point for both countdown and countup functions, both might
theoretically function, although countdown makes more sense. Conceptually, a countdown is a
series that lowers to a finish line of zero. As a result, it feels more natural to treat zero as the end
of a countdown than as the beginning of a count-up sequence. The software instantly initiates the
base condition and prints "Blastoff!" by routing the input of zero to countdown, thereby
terminating the process.
Screenshot 1
Screenshot 2
Part 2
Explanation
When code is written correctly (in terms of syntax) but tries to perform an impossible task, it is
called a "Runtime Error." The division by zero situations is the ideal illustration of this. Any
number divided by 0 is indefinable mathematically. The interpreter raises a ZeroDivisionError
and crashes the program right away if a Python program tries this computation (for example, 10 /
0) without protection.
A program crash is the worst thing that might happen to a user. Data loss, annoyance, and the
impression that the software is flawed are all brought on by it. We employ "Exception Handling"
through try and except blocks to avoid this.
The try block serves as a testing area. We embed "risky" code inside it—in this case, the division
operation. The code runs normally if the user inputs valid numbers. However, if the user enters
zero, Python stops the division immediately and jumps to the unless block. This enables us to
"catch" the issue before it causes the system to fail. For junior developers, it is vital to understand
that we are not only repairing problems; we are managing the flow of the application. By
catching the ZeroDivisionError, we may display a helpful message like "You cannot divide by
zero" instead of a complex technical traceback. This ensures that the application stays reliable,
polished, and user-friendly by transforming a significant failure into a useful learning
opportunity for the user.
Screenshot
Screenshot for Output 1 and 2
References
Bro Code. (2024, June 29). Learn Python EXCEPTION HANDLING in 5 minutes! [Video].
YouTube. [Link]
Downey, A. (2015). Think Python: How to think like a computer scientist (2nd ed.). Green Tree Press