Programming Assignment Unit 3
Programming Assignment Unit 3
Assignment Instructions:
In this unit, we explored the overview of the Conditionals and Recursion, which are the
prime requirements for the decision control process in a program. Before completing this
assignment, review the reading material below:
Q 1. The following is the countdown function copied from Section 5.8 of your textbook.
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!
Write a Python program that gets a number using keyboard input. (Remember to
use input for Python 3 but raw_input for Python 2.)
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
(countdown or countup) for input of zero.
Instructions:
Create a Python program that prompts the user to enter two numbers.
Implement a division operation on the entered numbers.
Introduce a condition that raises a runtime error if the second number is zero.
Provide an error message that clearly indicates the cause of the error.
Guide the junior developers in identifying the error message and implementing error
handling techniques to handle the division by zero scenario.
Questions:
Submission Instructions:
Reference
Downey, A. (2015). Think Python: How to think like a computer scientist. Green Tree Press
This assignment will be assessed by your instructor using the rubric available on the
assignment page located on the course homepage.
QN1
def countdown(n):
if n <= 0:
print('Blastoff!')
else:
print(n)
countdown(n-1)
def countup(x):
if x >= 0:
else:
print(x)
countup(x+1)
if number > 0:
countdown(number)
else:
countup(number)
Output:
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.
QN2
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
the junior can use the try and except statement. The try and except statements can handle
exceptions, which may happen when you run a program. Exceptions are errors that happen
For Example:
#Try and except statement checks if User tries to divide number by zero.
try:
print("Result: ",result)
except:
Output:
NOTE:
The division by zero error is a common error that occurs when you try to divide a number by
zero. This error can cause your program to crash or produce incorrect results.
Error handling is important because it allows you to deal with errors in a controlled way. By
handling errors, you can prevent your program from crashing and you can also provide your
In the example above, we use the `try` and `except` statements to handle the division by zero
error. The `try` statement tells the Python interpreter to try to execute the code inside the block.
If an error occurs, the Python interpreter will execute the code inside the `except` block.
In this case, the `except` block prints an error message “The number cannot be divided by zero”.
This message tells the user that the division by zero error has occurred.
If we did not handle the division by zero error, the Python interpreter would crash. This would
prevent the user from using the program and it would also make it difficult to debug the error.
Reference:
Learn Learn Scratch Tutorials, Syntax, Runtime and Logical Errors in Python
https://www.youtube.com/watch?v=ToPP5UGgJUM
Downey, A. (2015). Think Python: How to think like a computer scientist. Green Tree Press