2417944015 发表于 2020-8-22 11:52:58

Python超级超级简易计算器---闭包方法

还是要感谢Twilight6这位资深程序员,否则我还理解不了闭包的原理

print("Python简易计算器--闭包+Partition方式")
circle=input("请输入一个四则运算式(例如:8*400):")
if circle.partition("+")!=circle:
    def one(num1):
      def two(num2):
            return num1+num2
      return two
    temp=one(float(circle.partition("+")))
    print(circle.partition("+"),"+",circle.partition("+"),"=",str(temp(float(circle.partition("+")))))
if circle.partition("-")!=circle:
    def one(num1):
      def two(num2):
            return num1-num2
      return two
    temp=one(float(circle.partition("-")))
    print(circle.partition("-"),"-",circle.partition("-"),"=",str(temp(float(circle.partition("-")))))
if circle.partition("*")!=circle:
    def one(num1):
      def two(num2):
            return num1*num2
      return two
    temp=one(float(circle.partition("*")))
    print(circle.partition("*"),"*",circle.partition("*"),"=",str(temp(float(circle.partition("*")))))
if circle.partition("/")!=circle:
    def one(num1):
      def two(num2):
            return num1/num2
      return two
    temp=one(float(circle.partition("/")))
    if circle.partition("/")=="0":
      print("除数不能为零...程序即将报错停止")
      assert False
    print(circle.partition("/"),"/",circle.partition("/"),"=",str(temp(float(circle.partition("/")))))

一枚丶学渣 发表于 2020-8-22 13:00:35

脑瓜子嗡嗡的

页: [1]
查看完整版本: Python超级超级简易计算器---闭包方法