代码演示:
while True:
try:
op = input('请输入一个四则运算算式:')
if '+' in op :
a = op.split('+')
result = int(a[0]) + int(a[1])
print(result)
elif '-' in op:
a = op.split('-')
result = int(a[0]) - int(a[1])
print(result)
elif '*' in op:
a = op.split('*')
result = int(a[0]) * int(a[1])
print(result)
elif '/' in op:
a = op.split('/')
result = int(a[0]) / int(a[1])
print(result)
elif op == 'C':
print('感谢使用')
break
else:
raise Exception('请按a+b的格式输入')
except ZeroDivisionError:
print('除数不能为0')
except Exception as e:
print(e)
标签:实战,elif,int,split,计算器,result,简单,print,op From: https://blog.csdn.net/yijie_zhang7/article/details/142789054