使用高阶函数方式设计Calc函数,实现加、减、乘、除、乘方等计算功能
def add(x, y):
return x+y
def des(x, y):
return x-y
def cal(x, y):
return x*y
def dec(x, y):
return x/y
def pow(x, y):
return x**y
def Calc(f, x, y):
return f(x, y)
if __name__ == '__main__':
a = int(input('输入数字:'))
while True:
c = input('输入运算符:')
b = int(input('输入数字:'))
if c == '+':
a = Calc(add, a, b)
print('加法:', a)
elif c == '-':
a = Calc(des, a, b)
print('减法:', a)
elif c == '*':
a = Calc(cal, a, b)
print('乘法:', a)
elif c == '/':
a = Calc(dec, a, b)
print('除法:', a)
elif c == '**':
a = Calc(pow, a, b)
print('乘方:', a)
else:
break
实现了连续计算。