0% found this document useful (0 votes)
0 views8 pages

Programming Assignment Unit 3

This document outlines programming assignments focused on conditionals and recursion in Python. It includes instructions for creating recursive functions for counting up and down based on user input, as well as handling division operations with error management for division by zero. The assignment requires code implementation, output demonstration, and explanations for both tasks, emphasizing the importance of error handling in programming.

Uploaded by

SAYILE JAMES
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)
0 views8 pages

Programming Assignment Unit 3

This document outlines programming assignments focused on conditionals and recursion in Python. It includes instructions for creating recursive functions for counting up and down based on user input, as well as handling division operations with error management for division by zero. The assignment requires code implementation, output demonstration, and explanations for both tasks, emphasizing the importance of error handling in programming.

Uploaded by

SAYILE JAMES
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/ 8

Programming Assignments

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:

o Think Python: How to think like a computer scientist Chapter 5 – Conditionals


and recursion

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.

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.
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.

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:

 Provide a code demonstrating how to handle the division by zero error.


 Output demonstrating the runtime error, including the error message.
 Explain the significance of error handling in expressions or conditions, using the
division by zero scenario as an example. Discuss the potential impact of not handling
this error in a program.
 Please provide detailed explanations and code snippets to guide the junior
developers in understanding and addressing the division by zero error in Python
programs.

Submission Instructions:

 Submit the solutions to both questions in one document.


 The code and its output must be explained technically. The explanation can be
provided before or after the code. The descriptive part of your response must be at
least 200 words.
 Make sure your submission is double-spaced, using Times New Roman, 12-point
font, with 1” margins.
 Use sources to support your arguments. Use high-quality, credible, relevant sources
to develop ideas that are appropriate for the discipline and genre of the writing.
 Use APA citations and references to support your work. Add a reference list at the
end of the submission. For assistance with APA formatting, view Learning Resource
Center: Academic Writing.
 Your submission should be clearly written, concise, and well organized, and free of
spelling and grammar errors. The grading will be based on the quality of your
analysis, accurate solution of the problem and the quality of your writing.

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

#Asks for user input

number = int(input("Enter Number: "))

#countdown recursive function

def countdown(n):

if n <= 0:

print('Blastoff!')

else:

print(n)

countdown(n-1)

#Countup recursive function

def countup(x):

if x >= 0:

print ("Blast Off")

else:

print(x)

countup(x+1)

#checks if the number input is greater or less than zero

#if greater than zero it calls the countdown function

#if less than zeor it calls the count up function

if number > 0:
countdown(number)

else:

countup(number)

Output:

When user inputs a positive number “5”

When user inputs a negative number “-5”

When user inputs a zero “0”

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

#Enables user to input two numbers of choice


number1 = int(input("Enter Number: "))

number2 = int(input("Enter Number: "))

#Calculates the division of the two numbers and outputs result

result = number1 / number2

print("Result: ", result)

Output when User divides the 5 / 5

Assuming in scenario, the User divides the number 5 by a zero

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

error “ZeroDivisionError: division by zero” is presented.


(Python Tutorial, Try and Except in Python - Python Tutorial (pythonbasics.org)). TO solve this

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

during execution of the program.

For Example:

#Enables user to input two numbers of choice

number1 = int(input("Enter Number: "))

number2 = int(input("Enter Number: "))

#Try and except statement checks if User tries to divide number by zero.

try:

result = number1 /number2

print("Result: ",result)

except:

print('\"The number cannot be divided by zero\"')

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

users with more informative error messages.

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

Python Tutorial, Try and Except in Python - Python Tutorial (pythonbasics.org)

Downey, A. (2015). Think Python: How to think like a computer scientist. Green Tree Press

You might also like