Machine Problem in Python
- Create a calculator app
- The user will choose between the 4 math operations (Add, Subtract, Multiply and Divide)
- The application will ask for 2 numbers
- Display the result
- The application will ask again if the user wants to try again
- 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()