博客
关于我
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 unittest单元测试框架 TestSuite测试套件
    查看>>
    PYTHON调离线语音合成并实时播放
    查看>>
    python unittest高级特性!
    查看>>
    Python unittest:如何将标准输出消息临时重定向到缓冲区并测试其内容?
    查看>>
    Python urllib/Requests下载文件失败,但浏览器下载失败
    查看>>
    Python urllib2 文件上传问题
    查看>>
    Python urllib2.open 连接由对等错误重置
    查看>>
    python urllib2详解及实例
    查看>>
    Python url请求提示certificate verify failed unable to get local issuer certificate
    查看>>
    Python UTC 日期时间对象的 ISO 格式不包括 Z(祖鲁语或零偏移)
    查看>>
    python valueerror object2_python遇到错误记录
    查看>>
    python vars的作用
    查看>>
    Python vcrpy库:HTTP请求记录和重放
    查看>>
    Python virtualenv
    查看>>
    python vue3实现大文件分段续传(断点续传)--带暂停和继续功能
    查看>>
    Python WebDriver如何打印整个页面源(html)
    查看>>
    Python WebSocket自动化测试:构建高效接口测试框架
    查看>>
    Python Web开发
    查看>>
    Redis 配置文件杂项。
    查看>>
    Python web自动化测试 —— 文件上传
    查看>>