Home > Net >  Calculator: division by zero error, how do I fix that?
Calculator: division by zero error, how do I fix that?

Time:12-22

Division by zero error, how do I fix that?(zero/zero = error). How can I implement it into the calculator function.

import logging
def calculate(choice):
    if choice == "1":
        logging.info("Add")
        num1 = input("Enter the first number")
        num2 = input("Enter the second number") 
        return int(num1)   int(num2)
    if choice == "2":
        logging.info("Subtract")
        num1 = input("Enter the first number")
        num2 = input("Enter the second number")
        return int(num1) - int(num2)
    if choice == "3":
        logging.info("Multiply")
        num1 = input("Enter the first number")
        num2 = input("Enter the second number")
        return int(num1) * int(num2)
    if choice == "4":
        logging.info("Divide")
        num1 = input("Enter the first number")
        num2 = input("Enter the second number")
        return int(num1) / int(num2)       `#division by zero error, how do I fix that?`
choice = input("Choose 1 - Add 2 - Subtract 3 - Multiply 4 - Divide")
result = calculate(choice)
print(result)```

CodePudding user response:

You indicate in the comments that you want the user to be shown a message when they try to divide by zero.

Python already prints a message - the exception and traceback:

>>> 0 / 0
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ZeroDivisionError: division by zero

but you may also 'catch' this exception and provide your own, more user-friendly message. Notice Python helpfully tells you the kind of exception it threw, the ZeroDivisionError. That is the type of exception you want to catch. E.g.

try:
     0 / 0
except ZeroDivisionError:
     print("Cannot divide by zero")
   

Produces the output:

Cannot divide by zero

You can read in more detail about the try ... except statement in the Python docs.

CodePudding user response:

If you just want it to return "Error", then you can use this code for part 4:

if choice == "4":
        logging.info("Divide")
        num1 = input("Enter the first number")
        num2 = input("Enter the second number")
        return int(num1) / int(num2) if num2 != 0 else "Error, division by 0 is impossible."

I'm guessing this is what you mean, I've basically put in an if statement in so that "Error" is returned if the divisor is 0.

Alternatively you could use a try except solution.

CodePudding user response:

Here is one of the option to allow user to enter second number if it's 0 or type exit to exit:

if choice == "4":
    logging.info("Divide")
    num1 = input("Enter the first number")
    num2 = input("Enter the second number")
    if num2 == "0":
        # If second number is 0, loop until user enter non-zero number of exit
        print ("Second number cannot be zero")
        while (True):
            num2 = input("Enter non-zero number or type 'exit' to exit")
            if (num2 != "0" or num2 == "exit"):
                break
                
    return int(num1) / int(num2) if num2 != "exit" else None
  • Related