-2

I am creating a calculator program in python. It must get maximum 5 numbers from the user and then the user enters the calculation sign they want to use. Everything is working, the + the - and the *, but the division / does not work and it returns the following error:

CALCULATOR
ENTER NUMBER 1. TYPE SPACE TO ENTER CALCULATION4
ENTER NUMBER 2. TYPE SPACE TO ENTER CALCULATION7
ENTER NUMBER 3. TYPE SPACE TO ENTER CALCULATION
ENTER THE CALCULATION + - / */
THE RESULT IS
Traceback (most recent call last):
  File ".\calc.py", line 52, in <module>
    print  "THE RESULT IS", int(NUMBER1) / int(NUMBER2) / int(NUMBER3) / int(NUMBER4) / int(NUMBER5)
ZeroDivisionError: integer division or modulo by zero

The calculator program:

from goto import goto, label
print(   "CALCULATOR"    )
NUMBER1 = 0; NUMBER2 = 0; NUMBER3 = 0; NUMBER4 = 0; NUMBER5 = 0;

NUMBER1 = raw_input(   "ENTER NUMBER 1. TYPE SPACE TO ENTER CALCULATION"    )
if NUMBER1 == " ":
  goto .end;

NUMBER2 = raw_input(   "ENTER NUMBER 2. TYPE SPACE TO ENTER CALCULATION"   )
if NUMBER2 == " ":
  NUMBER2 = 0;
  goto .calculation;

NUMBER3 = raw_input(   "ENTER NUMBER 3. TYPE SPACE TO ENTER CALCULATION"   )

if NUMBER3 == " ":
  NUMBER3 = 0;
  goto .calculation;

NUMBER4 = raw_input(   "ENTER NUMBER 4. TYPE SPACE TO ENTER CALCULATION"   )
if NUMBER4 == " ":
  NUMBER4 = 0;
  goto .calculation;

NUMBER5 = raw_input(   "ENTER NUMBER 5. TYPE SPACE TO ENTER CALCULATION"   )
if NUMBER5 == " ":
  NUMBER5 = 0;
  goto .calculation;

label .calculation

CALCULATION = raw_input(    "ENTER THE CALCULATION + - / *")


if CALCULATION == "+":
  print  "THE RESULT IS", int(NUMBER1) + int(NUMBER2) + int(NUMBER3) + int(NUMBER4) + int(NUMBER5)

if CALCULATION == "-":
  print  "THE RESULT IS", int(NUMBER1) - int(NUMBER2) - int(NUMBER3) - int(NUMBER4) - int(NUMBER5)

if CALCULATION == "*":
  print  "THE RESULT IS", int(NUMBER1) * int(NUMBER2) * int(NUMBER3) * int(NUMBER4) * int(NUMBER5)

if CALCULATION == "/":
  print  "THE RESULT IS", int(NUMBER1) / int(NUMBER2) / int(NUMBER3) / int(NUMBER4) / int(NUMBER5)


label .end

print "";print  "------------------------------------";print "";
print   "PROGRAM END."
print  "THANK YOU FOR USING THE CALCULATOR."
print  "SEE YOU NEXT TIME."
print  "BYE!"

Why does the error occur only when dividing the numbers and how can I resolve it?

2 Answers 2

1

you can't divide a number by 0, the error is clear.

Your code is not complete, we can't help us more. Try to give us a short but complete example.

1
  • Hi. thank you. tha whole code is in the question. what example are you talking about? How can i fix that error? Jan 31, 2020 at 13:19
1

Agreed with the first answer, that you can't divide by 0 ever, the error is implying that. But on another note, you can consider implementing the floor division which works for int and non-int numbers. Although that's your personal preference and depends on what the program requires.

floor division is

 //
3
  • Hi thanks. What is the floor division? I just see // Feb 5, 2020 at 11:20
  • @Zekros Admines: floor division operator is // Feb 5, 2020 at 16:08
  • but still trying to understand why you have NUMBER2 and on wards set to 0? and I believe you don't need the ";" after each variable, that's mainly for the other OOP languages like C++, C, Java, C#.... Feb 5, 2020 at 16:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.