马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
还是要感谢Twilight6这位资深程序员,否则我还理解不了闭包的原理
print("Python简易计算器--闭包+Partition方式")
circle=input("请输入一个四则运算式(例如:8*400):")
if circle.partition("+")[0]!=circle:
def one(num1):
def two(num2):
return num1+num2
return two
temp=one(float(circle.partition("+")[0]))
print(circle.partition("+")[0],"+",circle.partition("+")[2],"=",str(temp(float(circle.partition("+")[2]))))
if circle.partition("-")[0]!=circle:
def one(num1):
def two(num2):
return num1-num2
return two
temp=one(float(circle.partition("-")[0]))
print(circle.partition("-")[0],"-",circle.partition("-")[2],"=",str(temp(float(circle.partition("-")[2]))))
if circle.partition("*")[0]!=circle:
def one(num1):
def two(num2):
return num1*num2
return two
temp=one(float(circle.partition("*")[0]))
print(circle.partition("*")[0],"*",circle.partition("*")[2],"=",str(temp(float(circle.partition("*")[2]))))
if circle.partition("/")[0]!=circle:
def one(num1):
def two(num2):
return num1/num2
return two
temp=one(float(circle.partition("/")[0]))
if circle.partition("/")[2]=="0":
print("除数不能为零...程序即将报错停止")
assert False
print(circle.partition("/")[0],"/",circle.partition("/")[2],"=",str(temp(float(circle.partition("/")[2]))))
|