博客
关于我
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中的urlencode和urldecode
    查看>>
    python中的with学习
    查看>>
    python中的write和read的用法,有无换行符
    查看>>
    Python中的“真“和“假“
    查看>>
    PYTHON中的主体对象标识
    查看>>
    Python中的修饰函数始终返回NONE
    查看>>
    Python中的元素相乘与矩阵相乘(附Demo)
    查看>>
    Python中的分组/聚类数字
    查看>>
    python中的十六进制字符串到字节数组
    查看>>
    Python中的单元测试框架:使用unittest进行有效测试
    查看>>
    Python中的压缩和解压缩模块:zipfile和tarfile
    查看>>
    python中的变量可以随时命名、随时赋值_python基础2
    查看>>
    PYTHON中的可变和不可变字符串
    查看>>
    Python中的多个元组到两对元组?
    查看>>
    Python中的多态性
    查看>>