patternpythonMinor
Multiple numbers calculator
Viewed 0 times
numberscalculatormultiple
Problem
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 o
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 o
Solution
There are some places where you can dictionaries instead of
Is often re-written as a dictionary. So, you could create a dictionary called
You can then you can do:
This tries getting the particular key which is a function. It then calls it. If the operator is not found, it defaults to the
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.Code Snippets
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")import operator
OPS = {
'+' : operator.add,
'-' : operator.sub,
...
}
ERROR = lambda arg1, arg2: print("input not recognized")def calculate(num1, num2, act):
return OPS.get(act, ERROR)(num1, num2)Context
StackExchange Code Review Q#150109, answer score: 3
Revisions (0)
No revisions yet.