0% found this document useful (0 votes)
13 views

A.A Programming Assignment Unit 3

Uploaded by

madandolataiwo
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

A.A Programming Assignment Unit 3

Uploaded by

madandolataiwo
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Answer to question 1:

```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()
```

For a positive number input:


```plaintext
Enter a number: 5
5
4
3
2
1
Blastoff!
```

For a negative number input:


```plaintext
Enter a number: -3
-3
-2
-1
Blastoff!
```
For zero input:
```plaintext
Enter a number: 0
Blastoff!
```

Explanation for choosing to call `countdown` for zero:


Since zero is neither positive nor negative, the choice between calling `countdown` or `countup`
for zero is arbitrary. In this implementation, `countdown` is chosen for zero input, which results
in the same "Blastoff!" message as with negative inputs. This choice simplifies the program
logic, but you could also choose to call `countup` for zero input with a similar effect.

1. Functions `countdown` and `countup`:


- Both functions are defined to handle counting either down (from a positive number to zero)
or up (from a negative number to zero).
- `countdown` takes a positive integer `n` as input and recursively counts down from `n` to 0,
printing each number along the way until it reaches 0, where it prints “Blastoff!”.
- `countup` takes a negative integer `n` as input and recursively counts up from `n` to 0,
printing each number along the way until it reaches 0, where it also prints “Blastoff!”.

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.

5. **The `if __name__ == “__main__”:` block:**


- This block ensures that the `main` function is only executed if the script is run directly, not if
it’s imported as a module into another script.
- It’s a common Python idiom to include executable code inside this block to define the script’s
behavior when run directly.

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:

A program that demonstrates handling the division by zero error:

```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”)

Result = num1 / num2


Print(“Result of division:”, result)

Except ZeroDivisionError as e:
Print€
```

Output demonstrating the runtime error, including the error message:


```
Enter the first number: 10
Enter the second number: 0
Error: Division by zero is not allowed
```

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
-

You might also like