Python Forum
Python calculator divide by zero help
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python calculator divide by zero help
#1
I'm a beginner at python and I am building a calculator for a project and have everything working as needed except one small part. I have to get a similar output The Result of 20.0/0.0=You cannot divide by Zero. I am not allowed to use try-except so I tried using if statement but I seem to be doing it wrong. My code is below I appreciate any tips or help.

loRange = int(input('Enter your Lower range: '))
hiRange = int(input('Enter your Higher range: '))
number_1 = int(input('Enter your first number: '))
number_2 = int(input('Enter your second number: '))

def add(x, y):
return x + y

def subtract(x, y):
return x - y

def multiply(x, y):
return x * y

def divide(x, y):
return x / y

if (loRange <= number_1 <= hiRange) and (loRange <= number_2 <= hiRange):
print('The Result of', number_1, '+' ,number_2, '=', add(number_1,number_2))
print('The Result of', number_1, '-' ,number_2, '=', subtract(number_1,number_2))
print('The Result of', number_1, '*' ,number_2, '=', multiply(number_1,number_2))
print('The Result of', number_1, '/' ,number_2, '=', divide(number_1,number_2))
else:
print("The input values are outside the input range")

if (number_1 == 0) or (number_2 == 0):
print('You cannot divide by Zero')

print('Thanks for using our calculator!')
Reply
#2
I think if you put your
def divide(x, y):
    if x == 0 or y == 0:
        print('You cannot divide by Zero')
    else:
        return x / y
you might have more luck.
Reply
#3
Thanks for the tip I'm a noob at python and this class is accelerated online.
Reply
#4
Technically you should only be preventing a divide BY zero (a zero in the denominator). Zero divided by zero is undefined and zero divided by any other number is zero.

In your case, you should only be guarding against y == 0.
"So, brave knights, if you do doubt your courage or your strength, come no further, for death awaits you all with nasty, big, pointy teeth!" - Tim the Enchanter
Reply
#5
Very good point.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Need some help trying to get my Python mortgage amortization calculator to work prope IamSirAskAlot 4 15,740 Feb-12-2024, 10:53 PM
Last Post: BerniceBerger
  New to python, trying to make a basic calculator AthertonH 2 1,139 Apr-14-2022, 03:33 PM
Last Post: AthertonH
  Divide a number by numbers in a list. Wallen 7 8,015 Feb-12-2022, 01:51 PM
Last Post: deanhystad
  How To Create A "Birthday Calculator" in Python? unigueco9 3 3,678 Oct-11-2021, 08:03 PM
Last Post: SamHobbs
  python calculator only using decomposing functions kirt6405 1 1,760 Jun-19-2021, 12:52 AM
Last Post: bowlofred
  how to divide one big equation entered as a string to small chunks gungurbuz 1 1,614 May-28-2020, 03:46 PM
Last Post: Larz60+
  how to divide array number chinting 5 2,853 Dec-30-2019, 11:29 AM
Last Post: ibreeden
  Python Program to Make a Simple Calculator jack_sparrow007 2 10,160 Oct-19-2018, 08:32 AM
Last Post: volcano63
  Python don't divide SteLu 8 5,216 Dec-21-2017, 02:58 PM
Last Post: casevh
  Creating a Calculator with Python KatherineHov 8 7,730 Aug-03-2017, 02:13 PM
Last Post: sparkz_alot

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020