Programming Assignment Unit 3
Programming Assignment Unit 3
May 1, 2025
2
Q1.
def countup(n):
else:
countup(n+1) #Call the function again with +1 combined with the argument.
countup(-3)
Output
-3
-2
-1
Blastoff
def countdown(n):
if n <= 0:
3
print('Blastoff!')
else:
print(n)
countdown(n-1)
if num > 0:
countdown(num)
countup (num)
else:
countdown(num)
4
Enter a number: 5
Blastoff!
Enter a number: -9
-9
-8
-7
5
-6
-5
-4
-3
-2
-1
Blastoff!
Enter a number: 0
Blastoff!
6
Explanation
I created two recursive functions: countdown and countup. The countdown function takes a
number n and counts down to 0. It prints each number as it counts down. When n reaches 0 or
goes below, the function prints "Blastoff!" and stops the counting. On the other hand, the
countup function does the reverse. It starts from a given number n and counts up to 0, printing
each number along the way. When n reaches 0 or goes below, it also prints "Blastoff!" and stops.
Next, I defined a function called blastoff(). This function asks the user to input a number. Since
the input function always returns a string, I used the int() function to convert that string into an
After getting the number, I used if, elif, and else statements to decide which function to run
based on the value of num. If num is greater than 0, it calls the countdown function. If num is
less than 0, it calls the countup function. If num is exactly 0, it calls the countdown function. I
chose to do this for consistency and readability, as it makes more sense to count down to zero
Q2.
if num2 == 0:
#If num2 is not zero, perform the division between num1 and num2
else:
result = num1/num2
division ()
Error handling is the safety procedure that programmers implement in their programs to handle
any unexpected errors or runtime errors that occur during the runtime process (TechTarget,
8
2022). Without proper error handling, the program would abruptly end when encountering a
runtime error such as a division by zero error. This situation could lead to data loss or corruption,
poor user experience, harder debugging processes, and security vulnerabilities (GeeksForGeeks,
2023).
By implementing error handling techniques, we can catch and gracefully handle errors. In the
codes above, the if conditional is used for catching the division by zero error when the second
number is zero. Instead of crashing, the program displays a meaningful error message and exits
Here, I first define a division () function that takes no argument. Then, I add two statements that
prompt the users to input two numbers. Since the input () function only returns string values, I
use the float () function to convert them into floating point values and store them in num1 and
num2 variables.
9
if num2 == 0:
Here, I add a condition that if num2 is zero, the function will raise an error and print out the
error
message "Error: Division by zero is not allowed.". As division by zero is not defined, the
#If num2 is not zero, perform the division between num1 and num2
else:
result = num1/num2
If there is no zero by division error, the function will perform division between two inputs and
Most programming languages provide mechanisms to handle runtime errors and exceptions. In
10
Python, we use the try-except construct to handle unexpected errors or exceptions that can occur
Alternative version of division that uses try and except for handling Zero Division Error.
Codes:
result = num1/num2
except ZeroDivisionError:
except:
division ()
Output:
Explanation
This is the second version of the division function that uses try-except for error handling instead
of conditionals. The function takes two inputs and performs division. If the user enters something
that isn’t a number, it will print “Please enter only a number.” If the user enters zero as the
second input, it will print “Error: Division by zero is not allowed” using except
ZeroDivisionError. In Python, try-except is used to handle runtime errors that happen while the
program is running. We can also specify except to catch specific types of runtime errors. In this
code, we use except ZeroDivisionError to catch division by zero errors, while another except
Reference
Downey, A. (2015). Think Python: How to think like a computer scientist. Green Tree Press.
https://greenteapress.com/thinkpython2/thinkpython2.pdf
https://www.geeksforgeeks.org/error-handling-in-c/
https://www.techtarget.com/whatis/definition/error-handling