Bug asking for input number

Arturo B a7xrturodev at gmail.com
Fri Nov 15 21:15:36 EST 2013


Hi! I hope you can help me.

I'm writting a simple piece of code.
I need to keep asking for a number until it has all this specifications:

- It is a number
- It's lenght is 3
- The hundred's digit differs from the one's digit by at least two

My problem is that I enter a valid number like: 123, 321, 159, 346... and it keeps asking for a valid number.

Here's mi code:

res = input('Give me a number --> ')
hundreds = int(res[0])
ones = int(res[2])

# checks if the user enters a valid number
while not res.isdigit() or not len(res) == 3 or abs(hundreds - ones) <= 2:
    res = input('Enter a valid number --> ')

Thanks for help!



More information about the Python-list mailing list