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

Here's a Python Program That Addresses Both Questions

The document presents a Python program that implements countdown and countup functions based on user input, along with a division operation that includes error handling for division by zero. It highlights the importance of error handling in programming to maintain user experience and prevent crashes. The document also provides example outputs for different inputs, illustrating the program's behavior.

Uploaded by

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

Here's a Python Program That Addresses Both Questions

The document presents a Python program that implements countdown and countup functions based on user input, along with a division operation that includes error handling for division by zero. It highlights the importance of error handling in programming to maintain user experience and prevent crashes. The document also provides example outputs for different inputs, illustrating the program's behavior.

Uploaded by

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

Here's a Python program that addresses both questions:

def countdown(j):
if j <= 0:
print('Blastoff!')
else:
print(j)
countdown(j - 1)
def countup(j):
if j >= 0:
print('Blastoff!')
else:
print(j)
countup(j + 1)
def main():
num = int(input("Enter a number: "))
if num > 0:
countdown(num)
elif num < 0:
countup(num)
else:
countup(num)
def division_operation():
try:
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
if num2 == 0:
raise ZeroDivisionError("Error: Division by zero is not allowed!")
result = num1 / num2
print("Result:", result)
except ZeroDivisionError as x:
print(x)
if _name_ == "_main_":
main()
division_operation()
try:
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
if num2 == 0:
raise ZeroDivisionError("Error: Division by zero is not allowed!")
result = num1 / num2
print("Result:", result)
except ZeroDivisionError as e:
print(e)
if __name__ == "__main__":
main()
division_operation()
Output for different inputs:
For a positive number:
Enter a number: 5
5
4
3
2
1
Blastoff!
Enter the first number: 10
Enter the second number: 2
Result: 5.0

For a negative number:


Enter a number: -3
-3
-2
-1
Blastoff!
Enter the first number: 5
Enter the second number: 0
Error: Division by zero is not allowed!

For zero:
Enter a number: 0
Blastoff!
Enter the first number: 8
Enter the second number: 0
Error: Division by zero is not allowed!
Explanation of choice for zero input: I decided to call the countup function for an
input of zero. This decision keeps consistency with the behavior for negative inputs,
counting "up" towards zero.
Explanation of error handling significance: Error handling is essential in
programming to gracefully handle unexpected situations. In the division by zero

scenario, without proper handling, the program would likely crash, leading to a poor
user experience. We can detect and handle errors by executing error-handling
techniques like try-except blocks, preventing crashes, and delivering meaningful
feedback to users. Neglecting error handling could result in unpredictable program
behavior, potential data loss, and system instability, sabotaging the program's
reliability and usability.
Reference:
Downey, A. (2015). Think Python: How to think like a computer scientist. Green Tree
Press.

You might also like