python 四则运算器
2017-06-14 本文已影响113人
faraway_com
#_*_coding:utf-8_*_
#两个操作数和一个操作符的计算器,加减乘除取余幂 + - * / % **
'''
def yunsuan(num1,sym,num2):
symbol = ['+','-','*','/','%','**']
if sym==symbol[0]:
return num1+num2
elif sym==symbol[1]:
return num1-num2
elif sym==symbol[2]:
return num1*num2
elif sym==symbol[3]:
return num1/num2
elif sym==symbol[4]:
return num1%num2
elif sym==symbol[5]:
return num1**num2
num1 = int(raw_input('请输入第一个操作数\n>'))
sym = raw_input('请输入操作符\n>')
num2 = int(raw_input('请输入第二个操作数\n>'))
result = yunsuan(num1,sym,num2)
print result
'''
def yunsuan(exp):
symbol = ['+','-','*','/','%','**']
yx = ''
for i in symbol:
if i in exp:
yx = i
if yx == '':
return 'exp is error!!'
num = exp.split(yx)
if '.' in num[0] or '.' in num[1]:
num[0] =float(num[0])
num[1] =float(num[1])
else:
num[0] =int(num[0])
num[1] =int(num[1])
if yx==symbol[0]:
return num[0]+num[1]
elif yx==symbol[1]:
return num[0]-num[1]
elif yx==symbol[2]:
return num[0]*num[1]
elif yx==symbol[3]:
return num[0]/num[1]
elif yx==symbol[4]:
return num[0]%num[1]
elif yx==symbol[5]:
return num[0]**num[1]
exp = raw_input('请输入一个表达式\n>')
result = yunsuan(exp)
print result