0% found this document useful (0 votes)
9 views4 pages

Name: Prince K. Saah University of The People: Countdown Countup

The document provides Python code examples for countdown and countup functions based on user input, as well as a division operation that demonstrates error handling for division by zero. It emphasizes the importance of using try-except blocks to manage runtime errors and improve user experience. The document aims to educate junior developers on handling errors effectively in their code.

Uploaded by

Prince Saah
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)
9 views4 pages

Name: Prince K. Saah University of The People: Countdown Countup

The document provides Python code examples for countdown and countup functions based on user input, as well as a division operation that demonstrates error handling for division by zero. It emphasizes the importance of using try-except blocks to manage runtime errors and improve user experience. The document aims to educate junior developers on handling errors effectively in their code.

Uploaded by

Prince Saah
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/ 4

Name : Prince K.

Saah

University of the People

Here is the code for the Python program that gets a number using keyboard input
and calls either the countdown or countup function based on the input:
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)

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

if num > 0:
countdown(num)
elif num < 0:
countup(num)
else:
print("Zero")
The output for the following inputs would be:
 Positive number: 5
 5
 4
 3
 2
 1
 Blastoff!
 Negative number: -3
 -3
 -2
 -1
 Blastoff!
 Zero: 0
 Zero
For input of zero, I chose to call the countup function because it is the more general
of the two functions and can handle both positive and negative inputs. The
countdown function only handles positive inputs, so it would not be as flexible if we
wanted to allow the user to input any integer.
Here is an example of a Python program that performs a division operation on two
numbers provided by the user, but contains a runtime error due to a division by
zero:
numerator = int(input("Enter the numerator: "))
denominator = int(input("Enter the denominator: "))

result = numerator / denominator


print("The result is:", result)
When this program is run, it will prompt the user to enter the numerator and
denominator. If the user enters a non-zero value for the denominator, the program
will run without any errors and print the result of the division. However, if the user
enters zero for the denominator, the program will raise a ZeroDivisionError runtime
error because division by zero is not allowed in Python.
To help junior developers learn about error handling in expressions and conditions,
you can guide them in diagnosing and fixing this error by suggesting that they add
error handling code to the program. For example, you could suggest adding a try-
except block to catch the ZeroDivisionError exception and print an error message
to the user. Here is an example of how the program could be modified to handle this
error:
numerator = int(input("Enter the numerator: "))
denominator = int(input("Enter the denominator: "))

try:
result = numerator / denominator
print("The result is:", result)
except ZeroDivisionError:
print("Error: Division by zero is not allowed.")
By adding this error handling code, the program will no longer raise a runtime error
if the user enters zero for the denominator. Instead, it will print an error message to
the user and terminate the program. This can help junior developers learn about
how to handle errors in Python and write more robust code.
Here is an example of a Python program that performs a division operation on two
numbers provided by the user, but contains a runtime error due to a division by
zero:
numerator = int(input("Enter the numerator: "))
denominator = int(input("Enter the denominator: "))

result = numerator / denominator


print("The result is:", result)
When this program is run, it will prompt the user to enter the numerator and
denominator. If the user enters a non-zero value for the denominator, the program
will run without any errors and print the result of the division. However, if the user
enters zero for the denominator, the program will raise a ZeroDivisionError runtime
error because division by zero is not allowed in Python.
To help junior developers learn about error handling in expressions and conditions,
you can guide them in diagnosing and fixing this error by suggesting that they add
error handling code to the program. For example, you could suggest adding a try-
except block to catch the ZeroDivisionError exception and print an error message
to the user. Here is an example of how the program could be modified to handle this
error:
numerator = int(input("Enter the numerator: "))
denominator = int(input("Enter the denominator: "))

try:
result = numerator / denominator
print("The result is:", result)
except ZeroDivisionError:
print("Error: Division by zero is not allowed.")
By adding this error handling code, the program will no longer raise a runtime error
if the user enters zero for the denominator. Instead, it will print an error message to
the user and terminate the program. This can help junior developers learn about
how to handle errors in Python and write more robust code.
Here is an example of a Python program that prompts the user to enter two
numbers, performs a division operation on the entered numbers, and raises a
runtime error if the second number is zero:
numerator = int(input("Enter the numerator: "))
denominator = int(input("Enter the denominator: "))

result = numerator / denominator

print("The result is:", result)


When this program is run, it will prompt the user to enter the numerator and
denominator. If the user enters a non-zero value for the denominator, the program
will run without any errors and print the result of the division. However, if the user
enters zero for the denominator, the program will raise a ZeroDivisionError runtime
error because division by zero is not allowed in Python.
To guide junior developers in identifying the error message and implementing error
handling techniques to handle the division by zero scenario, you can suggest adding
a try-except block to catch the ZeroDivisionError exception and print an error
message to the user. Here is an example of how the program could be modified to
handle this error:
numerator = int: "))
denominator = int(input("Enter the denominator: "))

try:
result = numerator / denominator
print("The result is:", result)
except ZeroDivisionError:
print("Error: Division by zero is not allowed.")
By adding this error handling code, the program will no longer raise a runtime error
if the user enters zero for the denominator. Instead, it will print an error message to
the user and terminate the program. This can help junior developers learn about
how to handle errors in Python and write more robust code.
Here is an example of a Python program that demonstrates how to handle the
division by zero error:
numerator = int(input("Enter the numerator: "))
denominator = int(input("Enter the denominator: "))

try:
result = numerator / denominator
print("The result is:", result)
except ZeroDivisionError:
print("Error: Division by zero is not allowed.")
When this program is run, it will prompt the user to enter the numerator and
denominator. If the user enters a non-zero value for the denominator, the program
will run without any errors and print the result of the division. However, if the user
enters zero for the denominator, the program will raise a ZeroDivisionError runtime
error because division by zero is not allowed in Python.
To handle this error, the program uses a try-except block to catch the
ZeroDivisionError exception and print an error message to the user. This allows the
program to continue running even if the user enters an invalid value for the
denominator.
The significance of error handling in expressions or conditions is that it allows a
program to continue running even if an error occurs. In the division by zero
scenario, not handling this error could cause the program to crash or terminate
unexpectedly, which could be frustrating for the user and potentially lead to data
loss or other issues. By handling the error, the program can provide a more user-
friendly experience and ensure that it continues to function correctly even in the
presence of errors.
To guide junior developers in understanding and addressing the division by zero
error in Python programs, you can explain the concept of error handling and provide
examples of how to use try-except blocks to catch and handle specific types of
errors. You can also emphasize the importance of testing and debugging code to
ensure that it is robust and can handle unexpected inputs or errors.

You might also like