mercredi 4 août 2021

Validate user input is between two numbers [closed]

The code I am trying to understand is to do with input validation and I can follow it to a point but then it confuses me!

The entire code here:

input_ok = False

while not input_ok:
    user_input = input("Please enter a number between 1 and 10: ")
    if not user_input.isnumeric():
        print(user_input + " is not valid! Input must be numeric.")
    else:
        user_input = int(user_input)
        if 1 > user_input <= 10:
            print("Input not within valid range. (1 to 10).")
        else:
            print(str(user_input) + " is a valid input.")
            input_ok = True

The part I can seem to get my head around is:

           if 1 > user_input <= 10:
               print("Input not within valid range. (1 to 10).")
           else:
               print(str(user_input) + " is a valid input.")
               input_ok = True

For some reason, it seems backwards to me. The IF is checking that the user_input is between 1 and 10 I understand? If so it would come out as TRUE so why is it told to display an error message?

Aucun commentaire:

Enregistrer un commentaire