0% found this document useful (0 votes)
4 views5 pages

Programming Assignment Unit 3

The document outlines an assignment on conditionals and recursion in Python, where students must implement recursive functions for countdown and countup based on user input. It also discusses handling division by zero errors using try-except blocks to ensure program stability and improve user experience. The importance of error handling in programming is emphasized, particularly in critical systems.

Uploaded by

harrygamtel
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)
4 views5 pages

Programming Assignment Unit 3

The document outlines an assignment on conditionals and recursion in Python, where students must implement recursive functions for countdown and countup based on user input. It also discusses handling division by zero errors using try-except blocks to ensure program stability and improve user experience. The importance of error handling in programming is emphasized, particularly in critical systems.

Uploaded by

harrygamtel
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

Assignment Submission: Conditionals and Recursion in Python

Student Name: Harry Joof


Course: Collapse: CS 1101 - Programming Fundamentals
Date: 23rd September 2025

Question 1: Recursive Functions – countdown and countup


Explanation
In Python, recursion is a method where a function calls itself to solve smaller instances of a
problem. In Chapter 5 of Think Python (Downey, 2015), the function countdown(n) is introduced
as a recursive function that counts down from a given positive number to zero and then prints
"Blastoff!". To complement this, we are required to create a countup(n) function that starts from
a negative number and counts up to zero, ending with "Blastoff!".
In addition to implementing both functions, the program must ask the user for a number. If the
number is positive, it should call countdown. If it’s negative, it should call countup. For an input
of zero, I chose to call countdown, because semantically, "counting down to zero" makes sense
even if we start at zero so the output immediately prints "Blastoff!" without unnecessary steps.
This use of conditionals and recursion allows for effective decision-making and control flow in
the program.

Code
Example Outputs
Input: 3

Input: -3

Input: 0

Question 2: Handling Division by Zero Error

Explanation

A common runtime error in programming is a division by zero. Python raises a


ZeroDivisionError when a program attempts to divide a number by zero. To teach junior
developers the importance of error handling, we’ll deliberately trigger this error and then show
how to handle it using a try-except block.

Error handling is crucial for writing robust programs. If a division by zero is not handled, it can
crash the entire application. In critical systems (like financial applications or medical devices),
this could have serious consequences. Therefore, anticipating and managing errors ensures
program stability and improves user experience.

Using Python’s try-except structure, we can catch the ZeroDivisionError and provide a user-
friendly message or take corrective action.

Code with Intentional Runtime Error (for teaching purposes)

Sample Output (when denominator = 0):

Improved Code with Error Handling


Sample Output (when denominator = 0):

Importance of Error Handling

Error handling is not just a convenience it is essential to writing safe and dependable software. In
expressions like division, a zero denominator will raise an exception that terminates the program
if left unhandled. This is problematic in real-world applications, where uninterrupted operation is
critical.

By implementing try-except blocks, developers can preemptively catch such errors and guide the
user to provide valid input. This is particularly useful in user-facing applications where input
errors are common. Without error handling, even small issues can cause a program to crash,
leading to data loss, poor user experience, and potential system failures.

Error handling, therefore, is a cornerstone of resilient programming practices, especially in cases


involving user input, file handling, network communication, and numerical operations.

References

Downey, A. (2015). Think Python: How to Think Like a Computer Scientist (2nd ed.). Green
Tree Press.
Python Software Foundation. (2023). Errors and Exceptions. Retrieved from
[Link]

You might also like