0% found this document useful (0 votes)
9 views

UNIT 3 PROGRAMMING ASSIGNMENT

UNIVERSITY OF THE PEOPLE PROGRAMMING ASSIGNMENT
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)
9 views

UNIT 3 PROGRAMMING ASSIGNMENT

UNIVERSITY OF THE PEOPLE PROGRAMMING ASSIGNMENT
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
You are on page 1/ 10

School: University of the People

Moodle ID: C110223808

Class: CS 1101 - Programming Fundamentals

Unit 3: Conditionals and Recursion

Group: 0036.

Course instructor: Muhammad Anwar Shahid.


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!
- The base case is when n becomes 0 or greater. In this case, it prints 'Blastoff!'. If n is still

negative, the function:

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

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.

Provide the following.

 The code of your program.

 Respective output for the following inputs: a positive number, a negative number, and

zero.

 An explanation of your choice for what to call for input of zero.


CODE

OUTPUT – POSITIVE NUMBER


NEGATIVE NUMBER

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.

- Error handling is a critical component of software development, allowing programs to

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

by zero, it raises a ZeroDivisionError, causing the program to terminate abruptly. This

paper demonstrates how to handle division by zero using Python, discusses the

importance of error handling in programming, and explains the potential impact of

neglecting proper error management.

Code

# Function to perform division with error handling

def divide_numbers():
try:

# Prompt user for two numbers

num1 = float(input("22: "))

num2 = float(input("58: "))

# Perform division

result = num1 / num2

print(f"The result of division is: {result}")

except ZeroDivisionError as e:

# Handle division by zero

print("Error: Division by zero is not allowed.")

print(f"Technical details: {e}")

except ValueError:

# Handle non-numeric input

print("Error: Please enter valid numbers.")

except Exception as e:

# Handle any other unexpected errors

print(f"An unexpected error occurred: {e}")

finally:

print("Execution complete.")
Explanation of Code

1. User Input and Division:

The program prompts the user to enter two numbers and then attempts to divide the first

number by the second.

2. Error Handling with try-except:

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.

o The except ValueError block manages cases where non-numeric input is

provided.

o The except Exception block catches any other unexpected errors.

o The finally block runs regardless of whether an error occurred, indicating that the

execution is complete.

Output Demonstration

1. Normal Division (Input: 10 and 2):

Enter the first number: 10

Enter the second number: 2

The result of division is: 5.0

Execution complete.

2. Division by Zero (Input: 10 and 0):


vbnet

Copy code

Enter the first number: 10

Enter the second number: 0

Error: Division by zero is not allowed.

Technical details: division by zero

Execution complete.

3. Invalid Input (Input: "abc" and 2):

yaml

Copy code

Enter the first number: abc

Error: Please enter valid numbers.

Execution complete.

Significance of Error Handling

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

loss, system instability, or a poor user experience.

Potential Impact of Not Handling Division by Zero:

 Program Crashes: The program will halt execution, affecting reliability.


 User Frustration: Users may become frustrated if a program fails unexpectedly.

 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

overall user experience.

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.

Franklin, Beedle & Associates.

You might also like