Loading... <div class="panel panel-default collapse-panel box-shadow-wrap-lg"><div class="panel-heading panel-collapse" data-toggle="collapse" data-target="#collapse-4781be5bae5355e448097730cda8c47425" aria-expanded="true"><div class="accordion-toggle"><span style="">题目</span> <i class="pull-right fontello icon-fw fontello-angle-right"></i> </div> </div> <div class="panel-body collapse-panel-body"> <div id="collapse-4781be5bae5355e448097730cda8c47425" class="collapse in collapse-content"><p></p> ``` 需求:: 设计一个计算器类,可以进行加、减、乘、除计算。 ``` <p></p></div></div></div> ## 代码 ~~~ # @Time: 2021/10/15 13:02 # @Auth: 君若(QQ1813774883) class myCalc: def __init__(self, one, two): self.one = one self.two = two def addition(self, Retain): return round(self.one + self.two, Retain) def subtraction(self, Retain): return round(self.one - self.two, Retain) def multiplication(self, Retain): return round(self.one * self.two, Retain) def division(self, Retain): return round(self.one / self.two, Retain) while True: get_num1 = input("请输入第一个数字:") opera = input("请输入运算符:") get_num2 = input("请输入第二个数字:") get_retain = input("请输入保留小数位数:") num1 = float(get_num1) num2 = float(get_num2) retain = int(get_retain) result = 0.00 if opera == "+": result = myCalc(num1, num2).addition(retain) print("输出结果是:", result) elif opera == "-": result = myCalc(num1, num2).subtraction(retain) print("输出结果是:", result) elif opera == "*": result = myCalc(num1, num2).multiplication(retain) print("输出结果是:", result) elif opera == "/": result = myCalc(num1, num2).division(retain) print("输出结果是:", result) else: print("运算符错误!请重新输入") ~~~ 最后修改:2021 年 10 月 16 日 © 允许规范转载 打赏 赞赏作者 支付宝微信 赞 1 如果觉得我的文章对你有用,请随意赞赏