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

Recursive Functions and Error Handling in Python

Uploaded by

donnaelsaj
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)
11 views5 pages

Recursive Functions and Error Handling in Python

Uploaded by

donnaelsaj
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

Question 1: Recursive Functions – Countdown and Countup

In Think Python, Downey (2015) explains how recursion allows a function to call itself until a

base case is reached. This concept is used in the classic countdown function. Based on this, I

developed a complementary countup function that takes a negative integer and counts upward to

zero before printing “Blastoff!”

Program Code:

#Asks for user input

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

#countdown recursive function

def countdown(n):

if n <= 0:

print('Blastoff!')

else:

print(n)

countdown(n-1)

#Countup recursive function

def countup(x):

if x >= 0:

print ("Blast Off")

else:

print(x)
countup(x+1)

#checks if the number input is greater or less than zero

#if greater than zero it calls the countdown function

#if less than zeor it calls the count up function

if number > 0:

countdown(number)

else:

countup(number)

Output:

When user inputs a positive number “5”

When user inputs a negative number “-5”

When user inputs a zero “0”


When the user inputs zero “0” the code checks whether if the number inputted is greater than or

equal to zero. Since the number is equal to zero, the output Blast off will be printed.

Question 2: Division and Error Handling

Division by zero is one of the most common runtime errors in programming. In Python,

attempting this operation raises a ZeroDivisionError. Teaching junior developers to anticipate

and handle such exceptions is critical because unhandled runtime errors can cause a program to

crash unexpectedly.

Deliberate Error Example (without handling):

#Enables user to input two numbers of choice

number1 = int(input("Enter Number: "))

number2 = int(input("Enter Number: "))

#Calculates the division of the two numbers and outputs result

result = number1 / number2

print("Result: ", result)

Output when User divides the 5 / 5


Assuming in scenario, the User divides the number 5 by a zero

Output:

The above error is received; this is a runtime error. The number 5 cannot be divided by a zero,

however as the code is not able to print an output informing the user of this mistake. A runtime

error “ZeroDivisionError: division by zero” is presented.

For Example:

#Enables user to input two numbers of choice

number1 = int(input("Enter Number: "))

number2 = int(input("Enter Number: "))

#Try and except statement checks if User tries to divide number by zero.

try:

result = number1 /number2

print("Result: ",result)

except:

print('\"The number cannot be divided by zero\"')


Output:

Significance of Error Handling:

Error handling is essential because it makes programs more robust and user-friendly. If runtime

errors are not handled, the program may terminate abruptly, leading to poor user experience and

potential data loss. In the case of division by zero, error handling prevents crashes and provides

meaningful feedback to the user, guiding them to correct their input. By using try-except blocks,

developers ensure that unexpected input does not break the flow of execution. This practice not

only improves software reliability but also demonstrates professionalism in coding standards.

References

Downey, A. (2015). Think Python: How to think like a computer scientist. Green Tree Press.

You might also like