Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
85 views

Programming Assignment Unit 3

CS 1101-01 - AY2025-T1 Programing Assignment Unit 3
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
85 views

Programming Assignment Unit 3

CS 1101-01 - AY2025-T1 Programing Assignment Unit 3
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Q 1. The following is the countdown function copied from Section 5.

8 of your
textbook

Python Code:

def countdown(n):
if n <= 0:
print('Blastoff!')
else:
print(n)
countdown(n-1)

def countup(n):
if n >= 0:
print('Blastoff!')
else:
print(n)
countup(n+1)

def main():
n = int(input("Enter a number: "))

if n > 0:
countdown(n)
elif n < 0:
countup(n)
else:
print("Blastoff!")

main()
Output:

1. For a positive number (e.g., 3):

Output:

2. For a negative number (e.g., -3):


Output:

3. For Zero:

In this program, I have done the following:


 For a positive input, the program calls countdown, which counts down to zero
and then prints "Blastoff!"
 For a negative input, the program calls count-up, which counts up to zero from
the negative number and then prints "Blastoff!"
 For an input of zero, the program directly calls countdown(0), to print "Blastoff!"
since it’s essentially same as counting down from zero. This choice maintains
consistency in the behaviour of the program for all input values.

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.

Step 1: Program Deliberately Containing Division by Zero Error

Python Code:
def countdown(n):
if n <= 0:
print('Blastoff!')
else:
print(n)
countdown(n-1)
def countup(n):
if n >= 0:
print('Blastoff!')
else:
print(n)
countup(n+1)

def main():
n = int(input("Enter a number: "))

if n > 0:
countdown(n)
elif n < 0:
countup(n)
else:
print("Blastoff!")

main()

Example Output with Division by Zero:


Step 2: Code with Error Handling

Code with Exception Handling:

Output:

1. For valid division (e.g., 10 / 2):

2. For division by zero:

3. For invalid input(e.g., “a” as input):


Explanation:

Significance of Error Handling in Expressions and Conditions:

Error handling is essential to ensure that programs can recover from unexpected
situations without crashing. In the case of division by zero, attempting to divide any
number by zero causes a ZeroDivisionError in Python, which would crash the
program if unhandled.

By using try-except blocks, we can catch this error and provide a meaningful error
message to the user instead of letting the program crash. This improves user
experience and the program's robustness, especially when handling external input,
which is often unpredictable.

Impact of Not Handling the Error:

If the division by zero error is not handled, the program will terminate abruptly, and
users may lose confidence in the application’s reliability. In more critical systems,
unhandled errors like this could cause severe consequences, such as data corruption,
incomplete processes, or even security vulnerabilities.

You might also like