Machine Problem in Python

  1. Create a calculator app
  2. The user will choose between the 4 math operations (Add, Subtract, Multiply and Divide)
  3. The application will ask for 2 numbers
  4. Display the result
  5. The application will ask again if the user wants to try again
  6. Use the appropriate Exception (ex: Invalid input such as text and zero division)

Program Listing

# mod5_act2.py
# Author : Jake R. Pomperada

def calculator():
    validMenuOptions = ["+", "-", "*", "/", "e"]
    while True:
        displayMenu()

        menuSelection = input("Enter your Option: ")

        # Handling user's menu input
        if menuSelection not in validMenuOptions:
            print("[-] Error: Invalid Input!")
        elif menuSelection == "e":
            print("[+] Program Terminated!")
            break
        else:
            # Asking user to enter numbers
            try:
                firstNumber = float(input("Enter 1st Number: "))
                secondNumber = float(input("Enter 2nd Number: "))

                result = 0

                # Checking each possibility and storing the output in 'result' variable
                if menuSelection == "+":
                    result = firstNumber + secondNumber
                    print("[+] Answer: ", result)
                elif menuSelection == "-":
                    result = firstNumber - secondNumber
                    print("[+] Answer: ", result)
                elif menuSelection == "*":
                    result = firstNumber * secondNumber
                    print("[+] Answer: ", result)
                elif menuSelection == "/":
                    if secondNumber == 0:
                        print("[-] Error: Cannot divide by zero")
                    else:
                        result = firstNumber / secondNumber
                        print("[+] Answer: ", result)
            except:
                print("[-] Error: Invalid Input! Only numerical input is allowed.")



def displayMenu():
    print("----------------------------")
    print("        Menu        ")
    print("Enter (+) for Addition")
    print("Enter (-) for Subtraction")
    print("Enter (*) for Multiplication")
    print("Enter (/) for Division")
    print("Enter (e) to Exit")
    print("----------------------------")


if __name__ == "__main__":
    calculator()