马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
新手程序,有改进请评论哈:print('这是一个计算器,一会要退出在询问"计算几次"的时候输入"退出"即可')
while True:
times = input("您要计算几次:")
if times == "退出":
break
else:
try:
times = int(times)
except:
print("输入错误,自动退出......")
break
else:
answer1 = []
answer2 = []
ok = 1
for i in range(times+1):
try:
answer1.append(float((input("第%s个数是:" % int(i+1)))))
except:
ok = 0
print("输入错误,自动退出...")
break
if i != times:
answer2.append(input("符号是:"))
if ok == 0:
break
else:
answer3 = answer1[0]
for i in range(len(answer1)-1):
if answer2[i] == "+":
answer3 = answer3 + answer1[i+1]
elif answer2[i] == "-":
answer3 = answer3 - answer1[i+1]
elif answer2[i] == "*":
answer3 = answer3 * answer1[i+1]
elif answer2[i] == "/":
answer3 = answer3 / answer1[i+1]
else:
ok = 0
print("输入错误,自动退出...")
break
if ok == 0:
break
else:
print("得数为:",str(answer3))
print("已退出!")
|