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
#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


Messages In This Thread
RE: python calculator only using decomposing functions - by bowlofred - Jun-19-2021, 12:52 AM

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,799 Feb-12-2024, 10:53 PM
Last Post: BerniceBerger
  New to python, trying to make a basic calculator AthertonH 2 1,164 Apr-14-2022, 03:33 PM
Last Post: AthertonH
  How To Create A "Birthday Calculator" in Python? unigueco9 3 3,759 Oct-11-2021, 08:03 PM
Last Post: SamHobbs
  Python calculator divide by zero help dock1926 4 5,904 Jan-20-2020, 05:15 PM
Last Post: michael1789
  Python Program to Make a Simple Calculator jack_sparrow007 2 10,226 Oct-19-2018, 08:32 AM
Last Post: volcano63
  Creating a Calculator with Python KatherineHov 8 7,786 Aug-03-2017, 02:13 PM
Last Post: sparkz_alot
  Python Calculator Application MemeStealer 6 7,858 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