|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
虽然说是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[number]=="+":
circle_3=circle_2[:number]
circle_4=circle_2[number+1:]
infront=''
behind=''
for number_2 in range(number):
infront=infront+circle_3[number_2]
for number_2 in range(len(circle_2)-number-1):
behind=behind+circle_4[number_2]
print(infront+"+"+behind+"="+str(float(infront)+float(behind)))
if circle_2[number]=="-":
circle_3=circle_2[:number]
circle_4=circle_2[number+1:]
infront=''
behind=''
for number_2 in range(number):
infront=infront+circle_3[number_2]
for number_2 in range(len(circle_2)-number-1):
behind=behind+circle_4[number_2]
print(infront+"-"+behind+"="+str(float(infront)-float(behind)))
if circle_2[number]=="*":
circle_3=circle_2[:number]
circle_4=circle_2[number+1:]
infront=''
behind=''
for number_2 in range(number):
infront=infront+circle_3[number_2]
for number_2 in range(len(circle_2)-number-1):
behind=behind+circle_4[number_2]
print(infront+"*"+behind+"="+str(float(infront)*float(behind)))
if circle_2[number]=="/":
circle_3=circle_2[:number]
circle_4=circle_2[number+1:]
infront=''
behind=''
for number_2 in range(number):
infront=infront+circle_3[number_2]
for number_2 in range(len(circle_2)-number-1):
behind=behind+circle_4[number_2]
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[place_1+1:],"=",str(float(circle[:place_1])+float(circle[place_1+1:])))
if circle.find("-")!=-1:
place_1=circle.find("-")
print(circle[:place_1],"-",circle[place_1+1:],"=",str(float(circle[:place_1])-float(circle[place_1+1:])))
if circle.find("*")!=-1:
place_1=circle.find("*")
print(circle[:place_1],"*",circle[place_1+1:],"=",str(float(circle[:place_1])*float(circle[place_1+1:])))
if circle.find("/")!=-1:
place_1=circle.find("/")
if circle[place_1+1:]=="0":
print("除数不能为零...程序即将报错停止")
assert False
print(circle[:place_1],"/",circle[place_1+1:],"=",str(float(circle[:place_1])/float(circle[place_1+1:])))
第3种,partition分割三元数组方法
print("Python简易计算器--partition分割三元组方法")
circle=input("请输入一个四则运算式(800*4):")
if circle.partition("+")[0]!=circle:
print(circle.partition("+")[0],"+",circle.partition("+")[2],"=",str(float(circle.partition("+")[0])+float(circle.partition("+")[2])))
if circle.partition("-")[0]!=circle:
print(circle.partition("-")[0],"-",circle.partition("-")[2],"=",str(float(circle.partition("-")[0])-float(circle.partition("-")[2])))
if circle.partition("*")[0]!=circle:
print(circle.partition("*")[0],"*",circle.partition("*")[2],"=",str(float(circle.partition("*")[0])*float(circle.partition("*")[2])))
if circle.partition("/")[0]!=circle:
if circle.partition("/")[2]=="0":
print("除数不能为零...程序即将报错停止")
assert False
print(circle.partition("/")[0],"/",circle.partition("/")[2],"=",str(float(circle.partition("/")[0])/float(circle.partition("/")[2])))
第4种,函数方法,也用到了find和partition
print("Python简易计算器--字符串find方法")
circle=input("请输入一个四则运算式(800*4):")
if circle.find("+")!=-1:
def add(circle):
num1=float(circle.partition("+")[0])
num2=float(circle.partition("+")[2])
print(str(num1),"+",str(num2),"=",str(num1+num2))
add(circle)
if circle.find("-")!=-1:
def decrease(circle):
num1=float(circle.partition("-")[0])
num2=float(circle.partition("-")[2])
print(str(num1),"-",str(num2),"=",str(num1-num2))
decrease(circle)
if circle.find("*")!=-1:
def cheng(circle):
num1=float(circle.partition("*")[0])
num2=float(circle.partition("*")[2])
print(str(num1),"*",str(num2),"=",str(num1*num2))
cheng(circle)
if circle.find("/")!=-1:
def chu(circle):
num1=float(circle.partition("/")[0])
num2=float(circle.partition("/")[2])
if num2==0.0:
print("除数不能为零...程序即将报错停止")
assert False
print(str(num1),"/",str(num2),"=",str(num1/num2))
chu(circle)
|
|