-3

I am making calculator and for example when a user types a letter by accident instead of a number the program just crashes. How to make it run by saying "Error wrong key pressed" and it continues printing that message whenever a person types a letter. until the user type a number it will continue. For instance in my code I want to make the num1 and num2 print out "Error wrong key press" wehnever the person types a letter instead of a number.

edit: I also want the ending to print out a error message when one presses any key except for ""y" and "n"

here is my code

def calculator():

    print("")  # leaves a line space
    num1 = float((input("Enter first number ")))
    operator = input("Enter an operator ")
    num2 = float(input("Enter second number "))

    if num1 == str:
        print("Error: wrong key pressed")

    if num2 == str:
        print("Error: wrong key pressed")


    if operator == "+":
        print(num1 + num2)
    elif operator == "-":
        print(num1 - num2)
    elif operator == "*":
        print(num1 * num2)
    elif operator == "/":
        if num2 == 0:
            print("Error: You cannot divide a number by 0")
        else:
            print(num1 / num2)
    else:
        print("Error wrong key pressed")

    restart = input("Press \"y\" to continue. If you wish to leave press \"n\" ")

    if restart.lower() == "y":
        calculator()
    elif restart.lower() == "n":
        print("Bye!")
        exit()


calculator()
khelwood
  • 55,782
  • 14
  • 81
  • 108
Aces
  • 11
  • 2

2 Answers2

0

Just add a try catch around your input statements like this.

def calculator():

    print("")  # leaves a line space
    num1 = 0
    num2 = 0
    try:
        num1 = float((input("Enter first number ")))
        operator = input("Enter an operator ")
        num2 = float(input("Enter second number "))
    except Exception as e:
        print("Error: wrong key pressed")
        calculator()

    if num1 == str:
        print("Error: wrong key pressed")

    if num2 == str:
        print("Error: wrong key pressed")


    if operator == "+":
        print(num1 + num2)
    elif operator == "-":
        print(num1 - num2)
    elif operator == "*":
        print(num1 * num2)
    elif operator == "/":
        if num2 == 0:
            print("Error: You cannot divide a number by 0")
        else:
            print(num1 / num2)
    else:
        print("Error wrong key pressed")

    restart = input("Press \"y\" to continue. If you wish to leave press \"n\" ")

    if restart.lower() == "y":
        calculator()
    elif restart.lower() == "n":
        print("Bye!")
        exit()

calculator() 

Inside the try we write the statements and if an exception occur, it will be captured in the catch statement. In your case, we'll call the calculator program again.

PSN
  • 2,326
  • 3
  • 27
  • 52
0

You can try to write a loop to take care of this.

Use this instead of your first few lines.

while True:
    try: num1 = float((input("Enter first number ")))
    except: print ("Error: wrong key pressed")
    else: break

operator = input("Enter an operator ")

while True:
    try: num2 = float(input("Enter second number "))
    except: print("Error: wrong key pressed")
    else: break
Joe Ferndz
  • 8,417
  • 2
  • 13
  • 33