lundi 8 juillet 2019

Python 2.7 IF statement with input that takes int & string

New to Python (2.7). I'm trying to collect a user input that can either be an int or string (i'm assuming this is possible, though I'm not 100% sure). When I get to the statement that I've posted below, any number I enter prints the 'invalid input' message and prompts me for a user input again, even if it's within the range I want it to be. Entering 'q' still works properly and quits the loop if entered.

I've tried changing that first IF statement to read (0 <= collectEyesBlade <= 9) ...with no luck either.

while True:
    collectEyesBlade = raw_input(
        "\nEnter desired blade number: ")
    if collectEyesBlade in [range(0,9)]:
        break
    elif collectEyesBlade.lower() == 'q':
        return
    else:
        print "\nInvalid input. Enter value between 0 and 9, or 'q' to quit"
        continue

Aucun commentaire:

Enregistrer un commentaire