FAQ
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!

Search Discussions

Discussion Posts

Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 1 of 6 | next ›
Discussion Overview
grouppython-list @
categoriespython
postedNov 16, '13 at 2:15a
activeNov 16, '13 at 3:15a
posts6
users4
websitepython.org

People

Translate

site design / logo © 2022 Grokbase