A.A Programming Assignment Unit 3
A.A Programming Assignment Unit 3
```python
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():
num = int(input("Enter a number: "))
if num > 0:
countdown(num)
elif num < 0:
countup(num)
else:
countdown(num) # You could choose to call either countdown or countup for zero.
if __name__ == "__main__":
main()
```
2. Function `main`:
- The `main` function serves as the entry point of the program.
- It prompts the user to enter a number using `input`, converts it to an integer, and assigns it to
the variable `num`.
- Based on the value of `num`, it decides whether to call `countdown`, `countup`, or either of
them for zero input.
3. Input handling:
- If the input number (`num`) is greater than 0, `countdown` is called with `num`.
- If the input number (`num`) is less than 0, `countup` is called with `num`.
- If the input number (`num`) is 0, `countdown` is called. This choice is arbitrary, as either
`countdown` or `countup` could be called for zero input.
4. Execution flow:
- When `countdown` or `countup` is called, it starts counting either down or up recursively
until it reaches 0.
- At 0, both functions print “Blastoff!” and return, ending the recursion.
Overall, the program provides a simple command-line interface for counting down, counting up,
or blasting off based on user input. It utilizes recursion to achieve the countdown and countup
functionality.
Answer to question 2:
```python
Try:
Num1 = float(input(“Enter the first number: “))
Num2 = float(input(“Enter the second number: “))
If num2 == 0:
Raise ZeroDivisionError(“Error: Division by zero is not allowed”)
Except ZeroDivisionError as e:
Print€
```
Explanation of the significance of error handling in expressions or conditions, using the division
by zero scenario as an example:
Error handling is crucial in programming as it helps prevent unexpected program termination and
provides a graceful way to handle errors. In the division by zero scenario, if we don’t handle the
error, the program will raise a runtime error and abruptly terminate, which can lead to a poor user
experience or even data loss in more complex applications.
By implementing error handling, we can detect when a division by zero occurs and gracefully
handle it by displaying an informative error message to the user. This improves the usability of
the program and helps users understand why the error occurred.
Without error handling, the program would crash, leaving the user confused and frustrated.
Additionally, in larger programs, an unhandled division by zero error could propagate through
the code, causing unexpected behavior or corrupting data, which can be difficult to debug and
fix. Therefore, error handling is essential for robust and reliable software development.
Reference
- Downey, A. (2015). Think Python: How to think like a computer scientist. Green Tree Press
- STEMpedia. https://ai.thestempedia.com/example/handling-runtime-errors-in-python-
pictoblox/
- Python Recursion – https://www.programiz.com/python-programming/recursion
-