Python——计算器—四则运算

  • Post author:
  • Post category:python


计算器

编写程序,实现计算器的四则运算功能。

def num(num1,num2):
    clu = input("选择计算功能(+,-,*,/):")
    print("输入num1:",num1)
    print("输入num1:",num2)
    if clu =="+":
        sum = num1 + num2
        print(num1,"+",num2,"=",sum)
    elif clu == "-":
        sum = num1 - num2
        print(num1,"-",num2,"=", sum)
    elif clu == "*":
        sum = num1 * num2
        print(num1,"*",num2,"=", sum)

    elif clu == "-":
        sum = num1 / num2
        print(num1,"/",num2,"=", sum)
num(3.2,7)
def add(num1, num2):
    result = num1+num2
    print(f"{num1}+{num2}={result}")

def sub(num1, num2):
    result = num1-num2
    print(f"{num1}-{num2}={result}")

def mul(num1, num2):
    result = num1*num2
    print(f"{num1}*{num2}={result}")

def div(num1, num2):
    result = num1/num2
    print(f"{num1}/{num2}={result}")

op = input("选择计算功能(+,-,*,/):")
num1 = float(input("输入num1:"))
num2 = float(input("输入num2:"))

switcher = {"+": lambda: add(num1, num2),
            "-": lambda: sub(num1, num2),
            "*": lambda: mul(num1, num2),
            "/": lambda: div(num1, num2)}

func = switcher.get(op, None)

if func:
    func()
else:
    print("请选择正确的计算!")



版权声明:本文为weixin_44775255原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。