Python Forum
python calculator only using decomposing functions
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
python calculator only using decomposing functions
#1
the problem I am having is that when user inputs / and second number as 0 it keeps going in loop i need to exit the loop but dont know where to put the exit loop condition also cannot use break to exit loop
#!/usr/bin/python3




def menu():
   print ("Please select the tyoe of operation you want to perform")
   print ("+ for addition")
   print ("- for subtraction")
   print ("* for multiplication")
   print ("/ for division")

def addition(x,y):
   sum=x+y
   return sum

def subtraction(x,y):
   yum=x-y
   return yum

def multiplication(x,y):
   multi=x*y
   return multi

def division(x,y):
   div=x/y
   return div

def iszero(num):
   while num == 0:
      return print("division by zero cannot be performed")
      num =float(input("please enter a number: "))
   return print ("the results are :", division(num,num2))

     

menu()

operat=input("Please enter operator (q to quit) :")
while operat != "q":

    num=float(input("enter first number: "))
    num2=float(input("enter second number: "))

    if operat == '+':
       print (num, "+", num2, "=", addition(num,num2))

    elif operat == '-':
       print ("result of calculation is :", subtraction(num,num2))

    elif operat == '*':
       print ("the result of calculation is :", multiplcation(num,num2)) 

    elif operat == '/':
       iszero(num2)
Reply
#2
The main program is responsible for input, but you don't validate that input until it's handed to the calculation function. So your choices at that point are to:

* Raise an exception and have the main program handle the exception
* Return data that is a signal to the main program that something is broken
* Have the calculation function ask for input until corrected
* Return invalid data

To me, the first one is the cleanest. Just let the division create an exception. But have the main program handle the exception and ask the user to re-enter the data. In fact, you're already looping and asking for data again anyway. So just handle the exception and tell the user you can't do that.

Something like:
...
    elif operat == '/':
        try:
            result = division(num,num2)
            print(f"The result of calculation is :{result}")
        except ZeroDivisionError:
            print("Cannot divide by zero.")
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,675 Feb-12-2024, 10:53 PM
Last Post: BerniceBerger
  New to python, trying to make a basic calculator AthertonH 2 1,114 Apr-14-2022, 03:33 PM
Last Post: AthertonH
  How To Create A "Birthday Calculator" in Python? unigueco9 3 3,573 Oct-11-2021, 08:03 PM
Last Post: SamHobbs
  Python calculator divide by zero help dock1926 4 5,756 Jan-20-2020, 05:15 PM
Last Post: michael1789
  Python Program to Make a Simple Calculator jack_sparrow007 2 10,104 Oct-19-2018, 08:32 AM
Last Post: volcano63
  Creating a Calculator with Python KatherineHov 8 7,654 Aug-03-2017, 02:13 PM
Last Post: sparkz_alot
  Python Calculator Application MemeStealer 6 7,607 Jul-21-2017, 08:42 PM
Last Post: MemeStealer

Forum Jump:

User Panel Messages

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