低配版计算器-加减乘除
class Operation:
def __init__(self,str_num1, str_num2, operator):
self.str_num1 = str_num1
self.str_num2 = str_num2
self.operator = operator
def get_result(self):
try:
if operator == "+":
return float(self.str_num1) + float(self.str_num2)
if operator == "-":
return float(self.str_num1) - float(self.str_num2)
if operator == "*":
return float(self.str_num1) * float(self.str_num2)
if operator == "/":
return float(self.str_num1) / float(self.str_num2)
except Exception as e:
print(str(e))
if __name__ == "__main__":
str_num1 = input("Please input the 1st number: ")
operator = input("Please input the +-*/: ")
str_num2 = input("Please input the 2nd number: ")
oper = Operation(str_num1, str_num2, operator)
res = oper.get_result()
print(res)