Python超级超级简易计算器4种写法
虽然说是4种,其实深究的话都算是1种第1种,数组切片方法,这个我昨天发过一次:
print("Python--简易计算器...")
circle=input("在右侧输入一个单次两项计算式子(例如1+2):")
circle_2=[]
for txt in circle:
circle_2.append(txt)
for number in range(len(circle_2)):
if circle_2=="+":
circle_3=circle_2[:number]
circle_4=circle_2
infront=''
behind=''
for number_2 in range(number):
infront=infront+circle_3
for number_2 in range(len(circle_2)-number-1):
behind=behind+circle_4
print(infront+"+"+behind+"="+str(float(infront)+float(behind)))
if circle_2=="-":
circle_3=circle_2[:number]
circle_4=circle_2
infront=''
behind=''
for number_2 in range(number):
infront=infront+circle_3
for number_2 in range(len(circle_2)-number-1):
behind=behind+circle_4
print(infront+"-"+behind+"="+str(float(infront)-float(behind)))
if circle_2=="*":
circle_3=circle_2[:number]
circle_4=circle_2
infront=''
behind=''
for number_2 in range(number):
infront=infront+circle_3
for number_2 in range(len(circle_2)-number-1):
behind=behind+circle_4
print(infront+"*"+behind+"="+str(float(infront)*float(behind)))
if circle_2=="/":
circle_3=circle_2[:number]
circle_4=circle_2
infront=''
behind=''
for number_2 in range(number):
infront=infront+circle_3
for number_2 in range(len(circle_2)-number-1):
behind=behind+circle_4
if behind=="0":
print("除数不能为零...程序即将报错停止")
assert False
print(infront+"/"+behind+"="+str(float(infront)/float(behind)))
第2种,字符串find方法
print("Python简易计算器--字符串find方法")
circle=input("请输入一个四则运算式(800*4):")
if circle.find("+")!=-1:
place_1=circle.find("+")
print(circle[:place_1],"+",circle,"=",str(float(circle[:place_1])+float(circle)))
if circle.find("-")!=-1:
place_1=circle.find("-")
print(circle[:place_1],"-",circle,"=",str(float(circle[:place_1])-float(circle)))
if circle.find("*")!=-1:
place_1=circle.find("*")
print(circle[:place_1],"*",circle,"=",str(float(circle[:place_1])*float(circle)))
if circle.find("/")!=-1:
place_1=circle.find("/")
if circle=="0":
print("除数不能为零...程序即将报错停止")
assert False
print(circle[:place_1],"/",circle,"=",str(float(circle[:place_1])/float(circle)))
第3种,partition分割三元数组方法
print("Python简易计算器--partition分割三元组方法")
circle=input("请输入一个四则运算式(800*4):")
if circle.partition("+")!=circle:
print(circle.partition("+"),"+",circle.partition("+"),"=",str(float(circle.partition("+"))+float(circle.partition("+"))))
if circle.partition("-")!=circle:
print(circle.partition("-"),"-",circle.partition("-"),"=",str(float(circle.partition("-"))-float(circle.partition("-"))))
if circle.partition("*")!=circle:
print(circle.partition("*"),"*",circle.partition("*"),"=",str(float(circle.partition("*"))*float(circle.partition("*"))))
if circle.partition("/")!=circle:
if circle.partition("/")=="0":
print("除数不能为零...程序即将报错停止")
assert False
print(circle.partition("/"),"/",circle.partition("/"),"=",str(float(circle.partition("/"))/float(circle.partition("/"))))
第4种,函数方法,也用到了find和partition
print("Python简易计算器--字符串find方法")
circle=input("请输入一个四则运算式(800*4):")
if circle.find("+")!=-1:
def add(circle):
num1=float(circle.partition("+"))
num2=float(circle.partition("+"))
print(str(num1),"+",str(num2),"=",str(num1+num2))
add(circle)
if circle.find("-")!=-1:
def decrease(circle):
num1=float(circle.partition("-"))
num2=float(circle.partition("-"))
print(str(num1),"-",str(num2),"=",str(num1-num2))
decrease(circle)
if circle.find("*")!=-1:
def cheng(circle):
num1=float(circle.partition("*"))
num2=float(circle.partition("*"))
print(str(num1),"*",str(num2),"=",str(num1*num2))
cheng(circle)
if circle.find("/")!=-1:
def chu(circle):
num1=float(circle.partition("/"))
num2=float(circle.partition("/"))
if num2==0.0:
print("除数不能为零...程序即将报错停止")
assert False
print(str(num1),"/",str(num2),"=",str(num1/num2))
chu(circle)
建议学习代码风格 放到代码框里
比如这样 weiter 发表于 2020-8-22 10:50
放到代码框里
get it 2417944015 发表于 2020-8-22 11:04
get it
谢谢采纳{:10_297:} {:5_106:}
页:
[1]