0

I want to write a simple calculator and I need to check which mathematical operator the user enters so that he does not enter a string or anything else. How to do this efficiently?

I tried to check this with conditional statements and loops, but the program does not output an error in case of incorrect input.

try and except operators are not suitable because the error occurs at the logical level, not at the level of the python interpreter.

def data_entry_validation(user_operator):
    operators_to_check = ('+', '-', '*', '/')
    correct_operator = []
    for operator in operators_to_check:
        if operator not in user_operator:
            continue
        elif operator in user_operator:
            correct_operator.append(operator)
            return print(correct_operator)
        elif not correct_operator:
            res = print(
                "Please enter one of the operators listed above!", end='\n'
                "Do not add a space or other post characters.")
            return res

I apologize if my code is hard to read and inefficient, I am a newbie. Any help would be appreciated)

Sakar
  • 1
  • 1

0 Answers0