博客
关于我
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 - 安装了扩展的远程 Webdriver
    查看>>
    python - 将字符串中的日期与今天的日期进行比较
    查看>>
    python - 数据描述符(class 内置 get/set/delete方法 )
    查看>>
    Python - 根据值绘制彩色网格
    查看>>
    Python - 正则表达式在括号之间获取数字
    查看>>
    Python - 正则表达式在括号之间获取数字
    查看>>
    Python - 正则表达式在括号之间获取数字
    查看>>
    Python - 类 __hash__ 方法和集合
    查看>>
    Python - 轻松将文本文件内容转换为字典值/键
    查看>>
    Python - 递归和列表
    查看>>
    python -- 小数据池 is和 == 再谈编码
    查看>>
    Python -- 算法实现
    查看>>
    python --- 元组(tuple)
    查看>>
    python --- 列表(list)
    查看>>
    python --- 协程
    查看>>
    python --- 字符串 str
    查看>>
    python ----元组方法以及修改细节
    查看>>
    python ----字典dict
    查看>>
    python / 解决 pyinstaller 打包后运行时提示找不到模块的问题
    查看>>
    Python 10 个习惯用法,短平快第一期
    查看>>