python实现计算器(2019-01-07)

# _*_coding:utf-8_*_

#需求:简单计算器实现

#定义运算法则

def add(x,y):

    return x + y

def subtract(x,y):

    return x - y

def multiply(x,y):

    return x * y

def dividd(x,y):

    return x / y

#用户输入

print ('')

num1 = int(input('请输入第一个数字:'))

num2 = int(input('请输入第二个数字:'))

#用户选择运算法则

print ('')

print ('**********')

print ('运算法则:')

print ('1.加法')

print ('2.减法')

print ('3.乘法')

print ('4.除法')

print ('**********')

print ('')

sign = int(input('请输入运算符号:'))

#输出结果

if sign ==1:

    print ('')

    print ('结果:')

    print ('{} + {} = {}'.format(num1,num2,add(num1,num2)))

elif sign ==2:

    print ('')

    print ('结果:')

    print ('{} - {} = {}'.format(num1,num2,subtract(num1,num2)))

elif sign ==3:

    print ('')

    print ('结果:')

    print ('{} * {} = {}'.format(num1,num2,multiply(num1,num2)))

elif sign ==4:

    print ('')

    print ('结果:')

    print ('{} / {} = {}'.format(num1,num2,dividd(num1,num2)))

else:

    print('请输入正确的运算符号')

你可能感兴趣的:(python实现计算器(2019-01-07))