博客
关于我
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/

    你可能感兴趣的文章
    Objective-c正确的写法单身
    查看>>
    Objective-C语法之代码块(block)的使用
    查看>>
    ObjectMapper - 实现复杂类型对象反序列化(天坑!)
    查看>>
    ObjectProperty 类的使用
    查看>>
    Objects.equals有坑
    查看>>
    Object常用方法
    查看>>
    Object方法的finalize方法
    查看>>
    Object类有哪些方法,hashcode方法的作用,为什么要重写hashcode方法?
    查看>>
    Object类有哪些方法?各有什么作用?
    查看>>
    Objenesis创建类的实例
    查看>>
    OBObjective-c 多线程(锁机制) 解决资源抢夺问题
    查看>>
    OBS studio最新版配置鉴权推流
    查看>>
    Obsidian 彩色标题
    查看>>
    Obsidian的使用-ChatGPT4o作答
    查看>>
    Obsidian笔记记录GPT回复的数学公式无缝转化插件Katex to mathjax
    查看>>
    ObsoleteAttribute 可适用于除程序集、模块、参数或返回值以外的所有程序元素。 将元素标记为过时可以通知用户:该元素在产品的未来版本中将被移除。...
    查看>>
    OC block声明和使用
    查看>>
    OC Xcode快捷键
    查看>>
    oc 中的.m和.mm文件区别
    查看>>
    OC 中的重写 OC中没有重载 以及隐藏
    查看>>