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

Abdul-Azeez Adeyinka UoPeople Computer Science Assignment

Uploaded by

adeyinka azeez
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)
8 views5 pages

Abdul-Azeez Adeyinka UoPeople Computer Science Assignment

Uploaded by

adeyinka azeez
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/ 5

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.


Answer
# Define a recursive function countup that expects a negative argument and counts “up” from that
number

Def countup(n):

If n >= 0:

Print(‘Blastoff!’)

Else:

Print(n)

Countup(n+1)

# Get a number using keyboard input

N = int(input(‘Enter a number: ‘))

#If the number is positive, call countdown

If n > 0:

Countdown(n)

# If the number is negative, call countup

Elif n < 0:

Countup(n)

#If the number is zero, choose either function

Else:

Countdown(n) or countup(n)

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.

Answer

```python

# Prompt the user to enter two numbers

Num1 = float(input(“Enter the first number: “))

Num2 = float(input(“Enter the second number: “))

# Perform a division operation on the entered numbers

Result = num1 / num2

# Print the result

Print(f”The result of {num1} / {num2} is {result}”)

```

However, this program has a potential runtime error if the user enters zero as the second number. A
runtime error is an error that occurs when the program is executed, and it prevents the program
from continuing normally. In this case, the runtime error is caused by a division by zero, which is
mathematically undefined and not allowed in Python. The error message that the program will
display is:
```python

Traceback (most recent call last):

File “main.py”, line 6, in <module>

Result = num1 / num2

ZeroDivisionError: float division by zero

```

The error message indicates the type of error (`ZeroDivisionError`), the cause of the error (`float
division by zero`), and the location of the error (`line 6, in <module>`). To help the junior developers
diagnose and fix the error, we can use the following steps:

1. Identify the cause of the error. In this case, the error is caused by dividing a number by zero,
which is not allowed in Python.

2. Implement error handling techniques to handle the error. One common technique is to use a `try-
except` block, which allows us to try a block of code and catch any exceptions that may occur. We
can use the `ZeroDivisionError` as the exception type to catch the specific error that we want to
handle.

3. Provide an alternative solution or a meaningful message to the user if the error occurs. For
example, we can print a message that tells the user that they cannot enter zero as the second
number, and ask them to enter a different number.

Here is a modified version of the program that uses a `try-except` block to handle the division by
zero error:

```python

# Prompt the user to enter two numbers

Num1 = float(input(“Enter the first number: “))

Num2 = float(input(“Enter the second number: “))

# Try to perform a division operation on the entered numbers

Try:

Result = num1 / num2

# Print the result


Print(f”The result of {num1} / {num2} is {result}”)

# Catch the division by zero error

Except ZeroDivisionError:

# Print a message to the user

Print(“You cannot enter zero as the second number. Please enter a different number.”)

```

The significance of error handling in expressions or conditions is that it allows us to prevent the
program from crashing or behaving unexpectedly when an error occurs. It also allows us to provide a
better user experience by informing the user of the error and how to avoid it. The potential impact
of not handling the division by zero error in this program is that the program will terminate abruptly
and display an unfriendly error message to the user, which may confuse or frustrate them. By
handling the error, we can ensure that the program continues to run smoothly and that the user
receives a clear and helpful message.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy