dingtao 发表于 2020-2-24 09:37:15

python学习简单计算器


#简单计算器
#定义简单的加减乘除计算器 add(+),subtract(-),multiply(x) ,divide(/)

def add(x, y):
    return x + y
def subtract(x, y):
    return x - y
def multiply(x, y):
    return x * y
def divide(x, y):
    if y == 0:
      return '分母不能为0, 计算无效'
    else:
      return x / y

print("请选择运算符号:")
print("+""-""x""/")

choice = input("请输入运算(+ - x /):")

num1 =int(input("请输入数字1:"))
num2 =int(input("请输入数字2:"))

if choice == '+':
    print(num1,'+',num2,'=',add(num1,num2))
elif choice == '-':
    print(num1,'-',num2,'=',subtract(num1,num2))
elif choice == 'x':
    print(num1,'x',num2,'=',multiply(num1,num2))
elif choice == '/':
    print(num1,'/',num2,'=',divide(num1,num2))
else:
    print("输入错误")
页: [1]
查看完整版本: python学习简单计算器