On 11/15/2013 08:15 PM, Arturo B wrote:> Hi! I hope you can help me.
You only set 'hundreds' and 'ones' the first time, when the loop goes
around those values never change. Also, I don't see any .isdigit()
before you call int(), which may make it error (maybe you just didn't
post the full code?). Also, I think your <= is flipped the wrong way.
The difference should be greater than or equal to 2 right?
Try something like this:
def is_valid_input(s):
""" Returns True if a number is a digit,
is 3 digits long,
and hundreds - ones is >= 2
"""
if not (s.isdigit() and (len(s) == 3)):
return False
hundreds = int(s[0])
ones = int(s[2])
return abs(hundreds - ones) >= 2
prompt = 'Give me a number --> '
res = input(prompt)
while not is_valid_input(res):
print('\nInvalid number!: {}\n'.format(res))
res = input(prompt)
...Of course you don't have to make it a function, I just did that
because it was going to be used more than once. If you need to actually
work with 'hundreds' and 'ones', you can rewrite it to suit your needs.
--
- Christopher Welborn <
cjwe...@live.com>
http://welbornprod.com