Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Programming Assignment Unit 3

Q1:

Part 1: The count up function

Part 2: Program
Input: Positive number

Input: negative number

Input: 0

Explanation:

The above program takes a number and depending on the number counts down or up. Here zero

is a special case because with zero, one cannot count up or down. So, my program shows a

message that says “for counting, we need a number not a zero”, that basically means that with
zero none of the up or down functions can be possible. Therefore, when user enters zero the

program does nothing except showing a message to the user.

Q 2:

 The code above is a program that takes two numbers from the user and divides the first

number by the second number. However, as it is known that no number can be divided by

zero. Dividing a number undefined in mathematics and it leads to contradiction (Alfeld,

1997). So, dividing or coding a program that divides two numbers, one needs to be

careful of this case.

 When a user tries to divide a number by zero, the program throws an error and asks the

user that division by zero is not possible. We can raise errors in Python in instance like

this, so we can avoid causing the program to crush.

 Error handling is crucial skill in programming since we can write the code without syntax

errors; however, there are always errors that can occur. Handling this unexpected error
can prevent causing the program to crush. Error and exception can always can occur and

it leads unexpected behavior in the program or stops the program (Awan, 2023).

However, there are certain techniques and tools in Python that we can use to avoid errors;

for example, try-and-catch, allows to catch the unexpected errors (Awan, 2023). We can

use try-and-catch to catch the ZeroDivisionError that occurs and print the error, and the

program shows the following a runtime-error instead of crushing.

 When user enters two numbers without the second number being zero, the program

divides the number and the result of division is in float because of the accuracy.
 Try-and-catch allow the programmer to catch the error before it causes the program to

crush. So, the try option allows the programmer to identify the error, for example in our

program, we have identified the error which to divide a number by zero and we throw

ourselves an error not the interpreter while running the program. Our program runs

smoothly and we also get an error when this error is caught. That’s why it is important to

understand exception and error handling while programming.

References:

Alfeld, P. (1997, February 17). Why can't we divide by zero? Math.Utah.edu. Retrieved February

21, 2024, from https://www.math.utah.edu/~pa/math/0by0.html#:~:text=The% 20reason

%20that%20the%20result,definition%20leads%20to%20a%20contradiction.&text=a

%3Dr*b.&text=r*0%3Da.,solution%20of%20equation%20(1).

Awan, A. A. (2023, February). Exception & Error Handling in Python. Datacamp. Retrieved

February 21, 2024, from https://www.datacamp.com/tutorial/exception-handling-python

You might also like