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

Assignment 3

This program demonstrates error handling in Python by implementing two recursive functions, countdown and countup, that count up or down based on a user-input number. It uses try/except blocks to gracefully handle potential errors like division by zero. By catching specific exception types like ZeroDivisionError, it provides customized error messages to help users correct mistakes in their input. The program is intended to help junior developers learn techniques for identifying errors and implementing proper error handling in their own programs.

Uploaded by

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

Assignment 3

This program demonstrates error handling in Python by implementing two recursive functions, countdown and countup, that count up or down based on a user-input number. It uses try/except blocks to gracefully handle potential errors like division by zero. By catching specific exception types like ZeroDivisionError, it provides customized error messages to help users correct mistakes in their input. The program is intended to help junior developers learn techniques for identifying errors and implementing proper error handling in their own programs.

Uploaded by

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

Question 1:

def countdown(n):

if n <= 0:

print('Blastoff!')

else:

print(n)

countdown(n - 1)

def countup(n):

if n >= 0:

print(n)

countup(n + 1)

else:

print('Blastoff!')

# Get a number from the user

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

# Decide which function to call based on the input

if user_input > 0:

countdown(user_input)

elif user_input < 0:

countup(user_input)

else:

print('Zero entered. Choosing countdown.')


Now, let's explain how this program works:

We have two functions, countdown and countup, both of which are recursive. countdown counts
down from a positive number until it reaches 0 or below, printing "Blastoff!" at the end. countup
counts up from a negative number until it reaches 0 or above, also printing "Blastoff!" at the end.

We use input to get a number from the user, which is stored in the user_input variable.

We then use conditional statements to decide which function to call based on the value of
user_input:

If user_input is positive, we call countdown.

If user_input is negative, we call countup.

If user_input is zero, we choose to call countdown (but you could choose countup as well; the
choice is somewhat arbitrary).
The program executes the chosen function, producing the respective output based on the user's
input.

Here's what the program would output for different inputs:

For a positive number, e.g., 5:

Blastoff!

For a negative number, e.g., -3:

-3

-2

-1

Blastoff!

For zero:

Blastoff!

Question 2:

try:

# Get two numbers from the user

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

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


# Attempt the division

result = numerator / denominator

# Check if the result is NaN (Not-a-Number)

if math.isnan(result):

raise ValueError("Result is not a number (NaN).")

# Print the result

print(f"Result: {result}")

except ZeroDivisionError as e:

print(f"Error: {e} - Division by zero is not allowed.")

except ValueError as e:

print(f"Error: {e}")

except Exception as e:

print(f"An unexpected error occurred: {e}")


Here's an explanation of how this program works:

We use a try...except block to capture potential exceptions that may occur during the program's
execution.

Inside the try block:

We get two numbers from the user, the numerator and the denominator.

We attempt the division operation: result = numerator / denominator.

We check if the result is NaN (Not-a-Number) using math.isnan(result). If it is NaN, we raise a


ValueError with an appropriate error message.
If there is a ZeroDivisionError, it means the user attempted to divide by zero. We catch this
exception and print an error message.

If there is any other exception (which would be unexpected), we catch it with the generic
Exception class and print a general error message.

By intentionally introducing the division by zero error, this program demonstrates the
importance of error handling. Junior developers can learn how to identify different types of
errors and implement appropriate error-handling techniques to gracefully handle exceptional
cases. The program also provides clear error messages to help users understand and correct their
input mistakes.

You might also like