博客
关于我
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实现查找给定节点数的树中可能的二叉搜索树的数量树算法(附完整源码)
    查看>>
    Objective-C实现查找链表的中间元素算法(附完整源码)
    查看>>
    Objective-C实现样条插值(附完整源码)
    查看>>
    Objective-C实现根据cpu和磁盘序列号生成注册码( 附完整源码)
    查看>>
    Objective-C实现格雷码序列算法(附完整源码)
    查看>>
    Objective-C实现桥接模式(附完整源码)
    查看>>
    Objective-C实现检查一个数字是否可以被另一个数字整除算法(附完整源码)
    查看>>
    Objective-C实现检查一年是否是闰年算法 (附完整源码)
    查看>>
    Objective-C实现检查三个点在 3D 中是否共线算法(附完整源码)
    查看>>
    Objective-C实现检查字符串是否包含字母表中所有字母的算法(附完整源码)
    查看>>
    Objective-C实现检查字符是否为字母算法(附完整源码)
    查看>>
    Objective-C实现检查数字是否为偶数算法(附完整源码)
    查看>>
    Objective-C实现检查数字是否为奇数算法(附完整源码)
    查看>>
    Objective-C实现检查给定图中是否存在循环算法(附完整源码)
    查看>>
    Objective-C实现检查给定字符串是否在camelCase中算法(附完整源码)
    查看>>
    Objective-C实现检查给定的字符串是否在kebabcase中算法(附完整源码)
    查看>>
    Objective-C实现检查给定的字符串是否在snake_case中算法(附完整源码)
    查看>>