博客
关于我
Python多分支实现四则运算器
阅读量:63 次
发布时间:2019-02-26

本文共 1300 字,大约阅读时间需要 4 分钟。

???????????

?????????????????????????????????????????????????????????????????????????????????????

????

  • ????

    • ??????????????????
    • ???????????????????????????????
    • ???????????????????????????????
  • ????

    • ??????????????????????Error???????
    • ??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)

    ????

    • ?????????????????????????
    • ?????????????Error??????????

    转载地址:http://cpr.baihongyu.com/

    你可能感兴趣的文章
    Python Turtle图形窗口不显示任何内容
    查看>>
    Python tweepy写入到sqlite3 db
    查看>>
    Python TypeError:格式字符串的参数不足
    查看>>
    Python UI自动化测试Page Objects企业级实战
    查看>>
    Python趣味编程:用代码创造无限可能
    查看>>
    Python UI自动化测试三方库扩展详解
    查看>>
    Python UI自动化测试与Web UI自动化测试框架详解
    查看>>
    python谷歌翻译,2021年9月10日亲测可用,一次可以翻译十万,强烈star
    查看>>
    Python UI自动化测试数据驱动实战
    查看>>
    Python UI自动化测试集成UnitTest
    查看>>
    Python UI自动化测试集成UnitTest
    查看>>
    python unicode-escape
    查看>>
    Python unittest mock:是否可以在测试时模拟方法默认参数的值?
    查看>>
    Python unittest单元测试框架 TestSuite测试套件
    查看>>
    PYTHON调离线语音合成并实时播放
    查看>>
    python unittest高级特性!
    查看>>
    Python unittest:如何将标准输出消息临时重定向到缓冲区并测试其内容?
    查看>>
    Python urllib/Requests下载文件失败,但浏览器下载失败
    查看>>
    Python urllib2 文件上传问题
    查看>>
    Python urllib2.open 连接由对等错误重置
    查看>>