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()