Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder5.xlned.com!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.045 X-Spam-Evidence: '*H*': 0.91; '*S*': 0.00; 'subject:number': 0.09; 'differs': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'len(res)': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'res': 0.16; 'true:': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'bit': 0.19; 'entered': 0.20; 'header:User-Agent:1': 0.23; 'help!': 0.26; 'least': 0.26; 'code:': 0.26; 'asking': 0.27; 'header:In-Reply- To:1': 0.27; "i'm": 0.30; 'easier': 0.31; 'piece': 0.31; 'probably': 0.32; 'checking': 0.33; 'problem': 0.35; 'hundreds': 0.35; 'but': 0.35; 'doing': 0.36; 'thanks': 0.36; 'two': 0.37; 'checks': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'hope': 0.61; 'break': 0.61; 'length': 0.61; 'simple': 0.61; "you're": 0.61; 'first': 0.61; 'header:Reply-To:1': 0.67; 'reply- to:no real name:2**0': 0.71; 'reply-to:addr:python.org': 0.84 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.1 cv=C6LQl2/+ c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=2LCq-pQfA8cA:10 a=29S_NPfUQNkA:10 a=ihvODaAuJD4A:10 a=OUOv7kDek9cA:10 a=8nJEP1OIZ-IA:10 a=EBOSESyhAAAA:8 a=8AHkEIZyAAAA:8 a=jW3UiJbH1JsA:10 a=_wkLygVGC2BFNYnDmMkA:9 a=kiqU-JzYf4wkEFFy:21 a=IZt7Zrrxlw31dGDJ:21 a=wPNLvfGTeEIA:10 X-AUTH: mrabarnett:2500 Date: Sat, 16 Nov 2013 02:33:51 +0000 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:24.0) Gecko/20100101 Thunderbird/24.1.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Bug asking for input number References: <94e9901a-82e4-43a4-81e1-c2a0f8e96fc8@googlegroups.com> In-Reply-To: <94e9901a-82e4-43a4-81e1-c2a0f8e96fc8@googlegroups.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: python-list@python.org 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: 42 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1384569234 news.xs4all.nl 15885 [2001:888:2000:d::a6]:48700 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:59583 On 16/11/2013 02:15, 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! > In the loop you're asking for the number but not doing the: hundreds = int(res[0]) ones = int(res[2]) bit for it. Also, after the number is entered for the first time, you're not first checking its length or that it's a number. It's probably easier just to use a break in a loop: while True: res = input('Give me a number --> ') if len(res) == 3 and res.isdigit() and abs(int(res[0]) - int(res[2])) >= 2: break