|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#原理就是---假设输入8+2后通过for循环把8+2拆成'8' '+' '2'并放到数组里,然后又用for循环来判断是哪一个运算符,后通过数组切片 shuzhu[0:2] 来取出运算符前和后的数字进行相应运算,最后打印结果
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))) |
评分
-
查看全部评分
|