函数的参数传递(计算器)

#计算器
def add(x,y):
  return x+y
def minus(x,y):
  return x-y
def mult(x,y):
  return x*y
def divi(x,y):
  return x/y
def get_on(s):
  if s=='加'or s=='+':
    return add
  elif s=='减'or s=='-':
    return minus
  elif s=='乘'or s=='*':
    return mult
  elif s=='除'or s=='/':
    return divi

def main():
a=input("请输入需要运算的数.例如:2 + 4:\n")
x,s1,y=a.split()
x,y=int(x),int(y)
fn=get_on(s1)
print("结果是:",fn(x,y))
main()

猜你喜欢

转载自www.cnblogs.com/python-book/p/10153106.html