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("/"))))) 脑瓜子嗡嗡的
页:
[1]