pythoncalculator

Menu for python calculator


I have created a calculator in python that can calculate the addition, subtraction, multiplication, division or modulus of two integers. What equation is executed is based on which number is type in from the menu, and I would like the user to be able to go back to the menu after an equation after being asked whether or not to "continue?". Would appreciate any help

print("MENU")
print("1. Add")
print("2. Subtract")
print("3. Multiply")
print("4. Divide")
print("5. Modulous")

menu = input("Enter your choice: ")
if int(menu) == 1:
 def additon(number1=int(input("Enter first number: ")), number2=int(input("Enter second number: "))):
    return(number1 + number2)
answer1 = additon()
print("Result:", answer1)

if int(menu) == 2:
 def subtraction(number1=int(input("Enter first number: ")), number2=int(input("Enter second number: "))):
    return(number1 - number2)
answer2 = subtraction()
print("Result: ", answer2)

if int(menu) == 3:
 def multiplication(number1=int(input("Enter first number: ")), number2=int(input("Enter second number: "))):
    return(number1 * number2)
answer3 = multiplication()
print("Result: ", answer3)


if int(menu) == 4:
 def division(number1=int(input("Enter first number: ")), number2=int(input("Enter second number: "))):
    return(number1 / number2)
answer4 = division()
print("Result: ", answer4)


if int(menu) == 5:
 def modulus(number1=int(input("Enter first number: ")), number2=int(input("Enter second number: "))):
    return(number1 % number2)
answer5 = modulus()
print("Result: ", answer5)

if int(menu) != 1 or 2 or 3 or 4 or 5:
 print("Not a valid option")

Solution

  • Wrap your code in a while loop. The following code should work.

    def additon(number1, number2):
        return (number1 + number2)
    
    def subtraction(number1, number2):
        return(number1 - number2)
    
    def multiplication(number1, number2):
        return(number1 * number2)
    
    def division(number1, number2):
        return(number1 / number2)
    
    def modulus(number1, number2):
        return(number1 % number2)
    
    x = True
    while (x):
        print("MENU")
        print("1. Add")
        print("2. Subtract")
        print("3. Multiply")
        print("4. Divide")
        print("5. Modulous")
    
        menu = input("Enter your choice: ")
        if int(menu) == 1:
            answer1 = additon(number1=int(input("Enter first number: ")),
                              number2=int(input("Enter second number: ")))
            print("Result:", answer1)
    
        elif int(menu) == 2:
            answer2 = subtraction(number1=int(input("Enter first number: ")),
                                  number2=int(input("Enter second number: ")))
            print("Result: ", answer2)
    
        elif int(menu) == 3:
            answer3 = multiplication(number1=int(input("Enter first number: ")),
                                     number2=int(input("Enter second number: ")))
            print("Result: ", answer3)
    
    
        elif int(menu) == 4:
            answer4 = division(number1=int(input("Enter first number: ")),
                               number2=int(input("Enter second number: ")))
            print("Result: ", answer4)
    
    
        elif int(menu) == 5:
            answer5 = modulus(number1=int(input("Enter first number: ")),
                              number2=int(input("Enter second number: ")))
            print("Result: ", answer5)
    
        elif int(menu) < 1 or int(menu) > 5:
            print("Not a valid option")
    
        go_again = input("Would you like to go again (y/n): ")
        if (go_again.lower()=="n"):
            x = False
        else:
            continue