支持形式 N1 操作符 N2 (其中N1和N2为×××和浮点型,操作符可以为'+','-','*','/','%','**')
#!/usr/bin/env python
from __future__ import division
import sys
def add(a,b):
print a+b
def subtra(a,b):
print a-b
def multi(a,b):
print a*b
def divi(a,b):
print a/b
def take_remain(a,b):
print a%b
def power(a,b):
print pow(a,b)
def judge(para1,para2):
while True:
cali = exp.split('%s' % para1)
if cali[0].strip() == '' or cali[1].strip() == '':
break
cali = map(float,cali)
para2(cali[0],cali[1])
anwser = raw_input("type 'no' to quit 'Enter' to continue: ")
if anwser == 'no':
sys.exit()
else:
break
while True:
exp = raw_input('Enter a expression: ')
if exp.find('+') != -1:
judge('+',add)
if exp.find('-') != -1:
judge('-',subtra)
if exp.find('*') != -1:
judge('*',multi)
if exp.find('/') != -1:
judge('/',divi)
if exp.find('%') != -1:
judge('%',take_remain)
if exp.find('**') != -1:
judge('**',power)
转载于:https://blog.51cto.com/wjcaiyf/1690832