本文共 1300 字,大约阅读时间需要 4 分钟。
?????????????????????????????????????????????????????????????????????????????????????
????
????
try-except?????????????????????????
round???????????class Calculator: def __init__(self, a, b): self.a = a self.b = b def addition(self, retain): return round(self.a + self.b, retain) def subtraction(self, retain): return round(self.a - self.b, retain) def multiplication(self, retain): return round(self.a * self.b, retain) def division(self, retain): return round(self.a / self.b, retain)while True: try: num1 = float(input('???????:')) num2 = float(input('???????:')) operator = input('??????:') retain = int(input('?????????:')) except ValueError: print("Error") break if operator not in ['+', '-', '*', '/']: print("Error") break result = Calculator(num1, num2).{ '+' if operator == '+' else '-' if operator == '-' else '*' if operator == '*' else '/' if operator == '/' else }(retain) print(result) 转载地址:http://cpr.baihongyu.com/