Calculator in python

后端 未结 7 849
别那么骄傲
别那么骄傲 2021-01-27 01:45

I am trying to make calculator that can solve expressions with basic 4 operators, like 1+2*3-4/5, however it does not work and I do not know what is wrong. Please check my code.

7条回答
  •  花落未央
    2021-01-27 02:19

    Here is a simple python calculator program, feel free to use it:

    #Python calculator
    
    def menu():
        print ("Welcome to calculator.py")
        print ("your options are:")
        print (" ")
        print ("1) Addition")
        print ("2) Subtraction")
        print ("3) Multiplication")
        print ("4) Division")
        print ("5) Quit calculator.py")
        print (" ")
        return input ("Choose your option: ")
    
    def add(a,b):
        print (a, "+", b, "=", a + b)
    
    def sub(a,b):
        print (b, "-", a, "=", b - a)
    
    def mul(a,b):
        print (a, "*", b, "=", a * b)
    
    def div(a,b):
        print (a, "/", b, "=", a / b)
    
    loop = 1
    choice = 0
    while loop == 1:
        choice = menu()
        if choice == 1:
            add(input("Add this: "),input("to this: "))
        elif choice == 2:
            sub(input("Subtract this: "),input("from this: "))
        elif choice == 3:
            mul(input("Multiply this: "),input("by this: "))
        elif choice == 4:
            div(input("Divide this: "),input("by this: "))
        elif choice == 5:
            loop = 0
    
    print ("Thank you for using calculator.py!")
    

提交回复
热议问题