【Python习题】四则运算计算器(简易版)

题目内容

设计一个可进行四则运算的简易计算器。

个人解法

a = input("input the first number:\n")
b = input("input the second number:\n")

def addition(a, b):
    c = a + " + " + b
    print(c,"=",eval(c))
    
def subtraction(a, b):
    c = a + " - " + b
    print(c,"=",eval(c))
    
def multiply(a, b):
    c = a + " * " + b
    print(c,"=",eval(c))
    
def divison(a, b):
    if b != "0":
        c = a + " / " + b
        print(c,"=",eval(c))
    else:
        print("error")
        
while True:
    print("""
    Functions as follows:
        1. addition
        2. subtraction
        3. multiply
        4. division
        0. quit
    """)
    choice = input("Choose the functions:")
    if choice == "1":
        addition(a, b)
    elif choice == "2":
        subtraction(a, b)
    elif choice == "3":
        multiply(a, b)
    elif choice == "4":
        divison(a, b)
    elif choice == "0":
        print("bye")
        break
    else:
        print("error")

运行结果

【Python习题】四则运算计算器(简易版)_第1张图片

 本人拙作,请大佬们点评。

你可能感兴趣的:(Python习题,python)