博客
关于我
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中使用pip install ncmbot时各种错误解决办法
    查看>>
    Python中使用requests和parsel爬取喜马拉雅电台音频
    查看>>
    Python中使用turtle绘制多个同心圆
    查看>>
    Python中使用turtle绘制棋盘详解
    查看>>
    Python中使用wordcloud生成漂亮的中文词云
    查看>>
    Python中使用wxpy定时机器人给微信好友发送每日一文
    查看>>
    Python中使用zip()并行迭代
    查看>>
    Python中使用推导式创建序列
    查看>>
    Python中信号处理程序和atexit处理程序的调用
    查看>>
    Python中关于txt的简单读写模式与操作
    查看>>
    Python中关于类和函数的初体验之"__init__"和"__str__"不是"_init_"和"_str_"
    查看>>
    python中函数接受参数为*args, **kwargs怎么办
    查看>>
    Python中切片的高效迭代
    查看>>
    python中列表乘法、eval、lambda、hasattr、assert、*(星)等的用法
    查看>>
    Python中制作词云的WordCloud参数详解
    查看>>
    Python中可用和使用的系统内存?
    查看>>
    python中存根文件(.pyi)的用途是什么?
    查看>>
    Python中实现ASCII码与字符相互转换
    查看>>
    Python中实现获取所有微信好友的头像并拼接成一张图片
    查看>>
    Python中对象的布尔值
    查看>>