eval 在去掉双引号上非常有用,都不用类型转换。
a = input('输入算式')
print('the result is {:.2f}'.format(eval(a)))
如果不用eval,用类型转换,就非常复杂,如下:
for v in b:
if a.find(v) !=-1:
print(v)
c = a.split(v)
print(c);
print(type(c[0]))
if v == '+':
result = int(c[0])+int(c[-1])
elif v == '-':
result =int(c[0]) - int(c[-1])
elif v == '*':
result = int(c[0])*int(c[-1])
elif v == '/':
result = int(c[0])/int(c[-1])
else:
print('error here')
print('the result is {:.2f}'.format(result))