I need to allow the user to input only numbers from 1111 to 9999 as raw_input in python. How do I create a constraint for this condition? Thanks
4 Answers
You cannot force the user to input any specific thing using raw_input(). You'll have to validate the user input and preferably use a while loop until user enters correctly.
This will work (assuming the user is not stupid to input non-digits).
i = int(raw_input('Please, enter a number between 1110 and 10000'))
while not(1110<i<10000):
i = int(raw_input('Dude, come on, read the instruction'))
intor a real numberfloator something else?num = sanitized_input('Type a number from 1111 to 9999:', int, 1111, 9999). I now use that function for my own programs.