Abdul-Azeez Adeyinka UoPeople Computer Science Assignment
Abdul-Azeez Adeyinka UoPeople Computer Science Assignment
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.
Respective output for the following inputs: a positive number, a negative number, and zero.
Def countup(n):
If n >= 0:
Print(‘Blastoff!’)
Else:
Print(n)
Countup(n+1)
If n > 0:
Countdown(n)
Elif n < 0:
Countup(n)
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.
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:
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
```
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
```
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
Try:
Except ZeroDivisionError:
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.