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("请选择正确的计算!")

你可能感兴趣的:(Python,实践题目,python)