马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
看小甲鱼的书看到了匿名函数,突然发现又可以写一个很简单的计算器
print("Python简易计算器--Lambda匿名函数写法...")
circle=input("请输入一个四则运算式子:")
if circle.find("+")!=-1:
all=lambda infront,behind:infront+behind
print(circle.partition("+")[0],"+",circle.partition("+")[2],"=",str(all(float(circle.partition("+")[0]),float(circle.partition("+")[2]))))
elif circle.find("*")!=-1:
all=lambda infront,behind:infront*behind
print(circle.partition("*")[0],"*",circle.partition("*")[2],"=",str(all(float(circle.partition("*")[0]),float(circle.partition("*")[2]))))
elif circle.find("-")!=-1:
all=lambda infront,behind:infront-behind
print(circle.partition("-")[0],"-",circle.partition("-")[2],"=",str(all(float(circle.partition("-")[0]),float(circle.partition("-")[2]))))
elif circle.find("/")!=-1:
all=lambda infront,behind:infront/behind
if circle.partition("/")[2]=="0":
print("除数不能为零...程序即将报错停止")
assert False
print(circle.partition("/")[0],"/",circle.partition("/")[2],"=",str(all(float(circle.partition("/")[0]),float(circle.partition("/")[2]))))
|