Show DevBest [PY] Calculator

Sway

Ruby and Python are married.
Dec 19, 2010
194
76
This is an extremely simple calculator I cooked up in Python out of boredom.

Source:


Snippet:
Code:
def main():
    choices = ['a', 's', 'm', 'd']
    print("Would you like to add [A], subtract [S], multiply [M], or divide [D]?")
    choice = input()
 
    if choice.lower() == choices[0]:
        print()
        add()
    elif choice.lower() == choices[1]:
        print()
        subtract()
    elif choice.lower() == choices[2]:
        print()
        multiply()
    elif choice.lower() == choices[3]:
        print()
        divide()
    else:
        print("I didn't get that..")
        time.sleep(2)
        main()

It took me about 30 minutes to create and so far only has addition, subtraction, division and multiplication. I do wish to add other stuff (square root, exponential, etc.) later on.

It has no true exception handling so far.

The code can be made more efficient.

UPDATE:
** Made the interface much nicer.
** Added the option to quit the program from the start of the program (you could have only exited out after you performed an action like add).

Source:


Snippet:
Code:
def main():
    choices = ['1', '2', '3', '4']
    quit1 = 'Q'
    print()
    print()
   
    print("""
Enter a number to perform that action.
 
[1] Addition
[2] Subtraction
[3] Multiplication
[4] Division
 
[Q] Quit.
""")
    choice = input("> ")
 
    if choice.lower() == choices[0]:
        print()
        add()
    elif choice.lower() == choices[1]:
        print()
        subtract()
    elif choice.lower() == choices[2]:
        print()
        multiply()
    elif choice.lower() == choices[3]:
        print()
        divide()
    elif choice.lower() == quit1:
        print()
        print("Ending program...")
        time.sleep(2)
        os._exit(1)
       
    else:
        print()
        print("I didn't get that..")
        time.sleep(2)
        main()

I will be using CMath for the following updates to calculate the square root, exponential values, complex numbers, logarithms, etc.

I also plan on implementing NumPy to calculate linear equations and such much easier.
 

j4ke

Member
Nov 4, 2012
364
97
Nice release, very useful. Although most computers come with a calculator program. Haha. Good work though.
 

jayk

Retired Habbotard.
Sep 4, 2013
517
94
Someone released this on my old forum, I downloaded it and started using it.
Thanks, I guess!
 

Users who are viewing this thread

Top