3
\$\begingroup\$

This is my first ever Python program so I would really like to know if it's any good (I mean if it is well writen), and if not then how to make it better. I would also like to know if there is a way to get all the veriables in one line without a space between them.

This calculator is basicly this calculator which I wrote in C++ upgraded and writen in Python.

def calculate(num1, num2, act):
    if(act=='+'):
        total=num1+num2
    elif(act=='-'):
        total=num1-num2
    elif(act=='*'):
        total=num1*num2
    elif(act=='/'):
        total=num1/num2
    else:
        print("input not recognized")
    return total

def calc2():
    user_input=input("Enter a num1 act num2 (with a space between them): ")   #Gets the values
    var1, action, var2=user_input.split()     #assigns the values into the variables
    if(action=='/' and var2==0):      #checks for division by 0
        print("YOU CAN'T DIVIDE BY ZERO!!!")
    else:
        print(calculate(float(var1), float(var2), action))     #calls the 'calculating' function, recives and prints the total of act

def calc3():
    user_input=input("Enter a num1 act1 num2 act2 num3 (with a space between them): ")      #Gets the values
    var1, action1, var2, action2, var3=user_input.split()           #assigns the values into the variables
    if(action1=='/' and var2==0 or action2=='/' and var3==0):      #checks for division by 0
        print("YOU CAN'T DIVIDE BY ZERO!!!")
    elif((action2=='*' or action2=='/') and (action1=='+' or action2=='-')):    #checks if act2 should be done before act1 (order of operation)
        total=calculate(float(var2), float(var3), action2)     #calls the 'calculating' function, recives the total of act2
        print(calculate(float(var1), float(total), action1))     #calls the 'calculating' function, assigns the total of act2 as num2, recives and prints the total of act1
    else:                                                             #act1 is done before act2 (order of operation)
        total=calculate(float(var1), float(var2), action1)         #calls the 'calculating' function, recives the total of act1
        print(calculate(float(total), float(var3), action2))     #calls the 'calculating' function, assigns the total of act1 as num1, recives and prints the total of act2

def main():
    amount=float(input("How many numbers? (2-3 numbers) "))
    if(amount==2):
        calc2()
    elif(amount==3):
        calc3()
    else:
        print("I SAID TWO OR THREE NUMBERS")

main()           #starts program

Input/Output

Input:2 + 3 * 4
Output:14

\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

There are some places where you can dictionaries instead of if/elif/else statements, I will analyze one here:

if(act=='+'):
    total=num1+num2
elif(act=='-'):
    total=num1-num2
elif(act=='*'):
    total=num1*num2
elif(act=='/'):
    total=num1/num2
else:
    print("input not recognized")

Is often re-written as a dictionary. So, you could create a dictionary called OPS and write:

import operator

OPS = {
    '+' : operator.add,
    '-' : operator.sub,
    ...
}
ERROR = lambda arg1, arg2: print("input not recognized")

You can then you can do:

def calculate(num1, num2, act):
    return OPS.get(act, ERROR)(num1, num2)

This tries getting the particular key which is a function. It then calls it. If the operator is not found, it defaults to the ERROR.

\$\endgroup\$
1
  • \$\begingroup\$ I would use a try..except KeyError instead of dict.get, but otherwise nice answer. \$\endgroup\$
    – Graipher
    Commented Dec 17, 2016 at 7:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.