0% found this document useful (0 votes)
7 views5 pages

Task 3

Uploaded by

theagriprof
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views5 pages

Task 3

Uploaded by

theagriprof
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Q1: Countdown and Countup Functions

The Python program that implements both the `countdown` and `countup` functions

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

# Get user input

number = int(input("Enter a number: "))

# Decide which function to call based on the input


if number > 0:

countdown(number)

elif number < 0:

countup(number)

else:

print("Zero input detected. Calling countdown function.")

countdown(number)

```

Output Examples

1. For a positive number (e.g., 3):

```

Enter a number: 3

Blastoff!

```

2. For a negative number (e.g., -3):


```

Enter a number: -3

-3

-2

-1

Blastoff!

```

3. For zero:

```

Enter a number: 0

Zero input detected. Calling countdown function.

Blastoff!

```

Explanation for Zero Input

For an input of zero, I chose to call the `countdown` function. This decision is based on the idea
that zero can be treated as a boundary condition. In the context of counting down, reaching zero
is a natural stopping point before the "Blastoff!" message. It provides a clear and consistent
output for users, maintaining the theme of countdowns.

Q2: Division by Zero Error Handling


● Python program that demonstrates how to handle a division by zero error:

```python

def divide_numbers():

try:

# Get user input

numerator = float(input("Enter the numerator: "))

denominator = float(input("Enter the denominator: "))

# Check for division by zero

if denominator == 0:

raise ValueError("Cannot divide by zero.")

# Perform division

result = numerator / denominator

print(f"The result of {numerator} divided by {denominator} is {result}.")

except ValueError as e:

print(f"Error: {e}")

# Call the function


divide_numbers()

```

Output Demonstrating the Runtime Error

If the user enters a denominator of zero, the output will be:

```

Enter the numerator: 10

Enter the denominator: 0

Error: Cannot divide by zero.

```

Significance of Error Handling

Error handling is crucial in programming as it allows developers to manage unexpected


situations gracefully. In the case of division by zero, if this error is not handled, the program will
crash, leading to a poor user experience and potential data loss. By implementing error handling,
we can provide informative feedback to users, guiding them to correct their input without
terminating the program unexpectedly. This practice enhances the robustness and reliability of
software applications, ensuring they can handle edge cases effectively

References

Downey, A. B. (2024). Think Python: How to Think Like a Computer Scientist (3rd ed.).
O'Reilly Media.

You might also like