Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.007 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; '"""': 0.07; 'function,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'rewrite': 0.09; 'subject:number': 0.09; 'differs': 0.16; 'len(res)': 0.16; 'once.': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'res': 0.16; 'code.': 0.18; 'header:User-Agent:1': 0.23; 'error': 0.23; 'help!': 0.26; 'post': 0.26; 'least': 0.26; 'code:': 0.26; 'asking': 0.27; 'values': 0.27; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; "i'm": 0.30; '(maybe': 0.31; 'piece': 0.31; 'problem': 0.35; 'hundreds': 0.35; 'christopher': 0.36; 'false': 0.36; 'from:addr:live.com': 0.36; "didn't": 0.36; 'thanks': 0.36; 'two': 0.37; 'checks': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'hope': 0.61; 'full': 0.61; 'course': 0.61; 'simple': 0.61; 'first': 0.61; 'more': 0.64; 'prompt': 0.68; "'hundreds'": 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Christopher Welborn Subject: Re: Bug asking for input number Date: Fri, 15 Nov 2013 20:47:12 -0600 References: <94e9901a-82e4-43a4-81e1-c2a0f8e96fc8@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: 68-113-89-245.dhcp.leds.al.charter.com User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.1.0 In-Reply-To: <94e9901a-82e4-43a4-81e1-c2a0f8e96fc8@googlegroups.com> X-Mailman-Approved-At: Sat, 16 Nov 2013 04:57:18 +0100 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 61 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1384574240 news.xs4all.nl 15878 [2001:888:2000:d::a6]:35547 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:59588 On 11/15/2013 08:15 PM, Arturo B wrote:> 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! > 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 http://welbornprod.com