0

How can I force the user to enter +, -, * or / on my calculator?

My calculator currently works, but I want to stop the user for example to enter string on int on the operation field.

while True:
    number1 = input("Enter First Number! ")
    try:
        number1 = float(number1)
        break
    except ValueError:
        print("Enter a number")

Operation = input("Enter Operation! ")

while True:
    number2 = input("Enter Second Number! ")
    try:
        number2 = float(number2)
        break
    except ValueError:
        print("Enter a Number")

if Operation == "+":
    result = float(number1) + float(number2)
    print(result)

elif Operation =="-":
    result = float(number1) - float(number2)
    print(result)

elif Operation == "*":
    result = float(number1) * float(number2)
    print(result)

elif Operation == "/":
    result = float(number1) / float(number2)
    print(result)

else:
    print("Incorrect Operation")
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
JPimenta
  • 3
  • 2
  • As far as I know, there's no way to force a user to enter something. – M-Chen-3 Apr 09 '21 at 20:47
  • You do the same thing you do with numbers, just with a different check…!? `if Operation in ('+', '-', '*', '/'): break`… – deceze Apr 09 '21 at 20:48
  • thanks for the comment. would i put the if statement just below the operation variable? when i try i get an error. – JPimenta Apr 09 '21 at 21:04

0 Answers0