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


Messages In This Thread
Python calculator divide by zero help - by dock1926 - Jan-19-2020, 09:59 PM

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,863 Feb-12-2024, 10:53 PM
Last Post: BerniceBerger
  New to python, trying to make a basic calculator AthertonH 2 1,194 Apr-14-2022, 03:33 PM
Last Post: AthertonH
  Divide a number by numbers in a list. Wallen 7 8,184 Feb-12-2022, 01:51 PM
Last Post: deanhystad
  How To Create A "Birthday Calculator" in Python? unigueco9 3 3,831 Oct-11-2021, 08:03 PM
Last Post: SamHobbs
  python calculator only using decomposing functions kirt6405 1 1,805 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,657 May-28-2020, 03:46 PM
Last Post: Larz60+
  how to divide array number chinting 5 2,975 Dec-30-2019, 11:29 AM
Last Post: ibreeden
  Python Program to Make a Simple Calculator jack_sparrow007 2 10,274 Oct-19-2018, 08:32 AM
Last Post: volcano63
  Python don't divide SteLu 8 5,360 Dec-21-2017, 02:58 PM
Last Post: casevh
  Creating a Calculator with Python KatherineHov 8 7,857 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