UNIT 3 PROGRAMMING ASSIGNMENT
UNIT 3 PROGRAMMING ASSIGNMENT
Group: 0036.
def countdown(n):
if n <= 0:
print('Blastoff!')
else:
print(n)
countdown(n-1)
Write a new recursive function countup that expects a negative argument and counts “up” from
that number. Output from running the function should look something like this:
>>> countup(-3)
-3
-2
-1
Blastoff!
- The base case is when n becomes 0 or greater. In this case, it prints 'Blastoff!'. If n is still
Prints the current value of n. Recursively calls itself with n + 1 to count "up" toward zero.
OUTPUT
Write a Python program that gets a number using keyboard input. (Remember to use input for
If the number is positive, the program should call countdown. If the number is negative, the
program should call countup. Choose for yourself, which function to call
Respective output for the following inputs: a positive number, a negative number, and
zero.
CODE
OUTPUT
Explanation of Choice for Zero
I chose to call countup for an input of zero because it provides a natural progression starting
from zero and counting up to positive numbers, making it more intuitive than counting down
from zero. Since zero is often seen as a neutral or starting point, counting up aligns better with
user expectations.
Q 2: You are developing a program that performs a division operation on two numbers provided
by the user. However, there is a situation where a runtime error can occur due to a division by
zero. To help junior developers learn about error handling in expressions and conditions, you
want to create a program deliberately containing this error and guide them in diagnosing and
fixing it.
gracefully manage unexpected conditions and continue functioning without crashing. One
common runtime error is division by zero. When a program attempts to divide a number
paper demonstrates how to handle division by zero using Python, discusses the
Code
def divide_numbers():
try:
# Perform division
except ZeroDivisionError as e:
except ValueError:
except Exception as e:
finally:
print("Execution complete.")
Explanation of Code
The program prompts the user to enter two numbers and then attempts to divide the first
o The try block contains the division operation, which is monitored for potential
errors.
o The except ZeroDivisionError block catches and handles the division by zero
scenario.
provided.
o The finally block runs regardless of whether an error occurred, indicating that the
execution is complete.
Output Demonstration
Execution complete.
Copy code
Execution complete.
yaml
Copy code
Execution complete.
Error handling is crucial in programming because it ensures that a program can manage
unexpected events without crashing. In the context of division by zero, failing to handle the
ZeroDivisionError would cause the program to terminate abruptly, potentially leading to data
Data Integrity Issues: Abrupt termination might lead to incomplete or corrupt data.
By implementing robust error handling, developers can provide informative error messages,
allowing users to understand what went wrong and how to resolve it. Additionally, error
handling enhances the maintainability and reliability of the software, contributing to a better
References
Beazley, D. M., & Jones, B. K. (2013). Python Cookbook: Recipes for Mastering Python 3.
O'Reilly Media.
Downey, A. B. (2016). Think Python: How to Think Like a Computer Scientist (2nd ed.).
O’Reilly Media
Downey, A. B. (2015). Think Python: How to Think Like a Computer Scientist. Green Tea Press.
Van Rossum, G., & Drake, F. L. (2001). Python Reference Manual. Network Theory Ltd.
Ceder, N., & Zelle, J. (2013). Python Programming: An Introduction to Computer Science.