博客
关于我
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:从列表中创建元组列表
    查看>>
    Python:从嵌套列表中删除重复项
    查看>>
    Python:从间隔映射到值
    查看>>
    Python:使用 shell=False 的子进程调用不起作用
    查看>>
    Python:修改 PATH 环境变量的平台无关方式
    查看>>
    Python:假与 0
    查看>>
    Python:函数使用
    查看>>
    Python:函数进阶
    查看>>
    Python:在整数的二进制表示中查找最长的二进制间隙
    查看>>
    Python:如何从二进制转换为 base 64 并返回?
    查看>>
    Python:如何以非阻塞方式读取子进程的标准输出
    查看>>
    Python:如何使用 plotly 制作阴影区域或交替背景颜色?
    查看>>
    Python:如何根据日期时间获取值的计数
    查看>>
    python:如何检测串行 COM 上的设备名称/ID
    查看>>
    Python:如何模拟 datetime.utcnow()?
    查看>>
    Python:如果DataFrames之间的其他值匹配,则对DataFrame中的值求和
    查看>>
    Python:字典思维
    查看>>
    Python:将 HEX 字符串转换为字节
    查看>>
    Python:将列表与范围列表合并
    查看>>
    Python:将列表列表初始化为一定大小,
    查看>>