Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #59583

Re: Bug asking for input number

Date 2013-11-16 02:33 +0000
From MRAB <python@mrabarnett.plus.com>
Subject Re: Bug asking for input number
References <94e9901a-82e4-43a4-81e1-c2a0f8e96fc8@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.2696.1384569234.18130.python-list@python.org> (permalink)

Show all headers | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Bug asking for input number Arturo B <a7xrturodev@gmail.com> - 2013-11-15 18:15 -0800
  Re: Bug asking for input number MRAB <python@mrabarnett.plus.com> - 2013-11-16 02:33 +0000
  Re: Bug asking for input number Terry Reedy <tjreedy@udel.edu> - 2013-11-15 21:37 -0500
  Re: Bug asking for input number Arturo B <a7xrturodev@gmail.com> - 2013-11-15 19:03 -0800
  Re: Bug asking for input number Christopher Welborn <cjwelborn@live.com> - 2013-11-15 21:15 -0600
  Re: Bug asking for input number Christopher Welborn <cjwelborn@live.com> - 2013-11-15 20:47 -0600

csiph-web