*Q1: Recursive Functions*
Here's the code for the `countup` function and the main program:
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:
print("Zero! Let's blast off anyway!")
print('Blastoff!')
if __name__ == "__main__":
main()
*Output:*
- For a positive number (e.g., 3):
```
```
3
2
1
Blastoff!
* For a negative number (e.g., -3):
```
-3
-2
-1
Blastoff!
- For zero:
```
```
Zero! Let's blast off anyway!
Blastoff!
**Explanation:**
For input of zero, I chose to print a message and then call `Blastoff!` directly. This is because
neither `countdown` nor `countup` is suitable for zero, as `countdown` would not print anything,
and `countup` would count up indefinitely. Instead, I decided to handle zero as a special case
and blast off directly.
**Q2: Error Handling**
Here's an example program that demonstrates a division by zero error and guides junior
developers in diagnosing and fixing it:
```python
def divide_numbers():
try:
num1 = float(input("Enter the dividend: "))
num2 = float(input("Enter the divisor: "))
result = num1 / num2
print(f"The result is: {result}")
except ZeroDivisionError:
print("Error: Division by zero is not allowed!")
except ValueError:
print("Error: Invalid input! Please enter a number.")
def main():
print("Division Program")
print("----------------")
divide_numbers()
if __name__ == "__main__":
main()
*Output:*
- When dividing by zero:
```
```
Error: Division by zero is not allowed!
* When entering invalid input:
```
Error: Invalid input! Please enter a number.
*Explanation:*
In this program, we use a `try`-`except` block to catch the `ZeroDivisionError` that occurs when
dividing by zero. We also catch `ValueError` in case the user enters invalid input. By handling
these exceptions, we can provide a more robust and user-friendly program. Junior developers
can learn from this example how to diagnose and fix division by zero errors using error handling
techniques.