Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #59582 > unrolled thread
| Started by | Arturo B <a7xrturodev@gmail.com> |
|---|---|
| First post | 2013-11-15 18:15 -0800 |
| Last post | 2013-11-15 20:47 -0600 |
| Articles | 6 — 4 participants |
Back to article view | Back to comp.lang.python
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
| From | Arturo B <a7xrturodev@gmail.com> |
|---|---|
| Date | 2013-11-15 18:15 -0800 |
| Subject | Bug asking for input number |
| Message-ID | <94e9901a-82e4-43a4-81e1-c2a0f8e96fc8@googlegroups.com> |
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!
[toc] | [next] | [standalone]
| From | MRAB <python@mrabarnett.plus.com> |
|---|---|
| Date | 2013-11-16 02:33 +0000 |
| Message-ID | <mailman.2696.1384569234.18130.python-list@python.org> |
| In reply to | #59582 |
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
[toc] | [prev] | [next] | [standalone]
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Date | 2013-11-15 21:37 -0500 |
| Message-ID | <mailman.2697.1384569481.18130.python-list@python.org> |
| In reply to | #59582 |
On 11/15/2013 9: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.
If you enter a 'valid' number at first try, it works fine.
> 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:
Look at that last condition *carefully*!!!!
> res = input('Enter a valid number --> ')
--
Terry Jan Reedy
[toc] | [prev] | [next] | [standalone]
| From | Arturo B <a7xrturodev@gmail.com> |
|---|---|
| Date | 2013-11-15 19:03 -0800 |
| Message-ID | <9df62e04-f88b-477f-ad5f-f72287250ea5@googlegroups.com> |
| In reply to | #59582 |
MRAB your solution is good thank you I will use it. Terry Eddy I saw my mistake about for example 2 <= 2, I think it's easier to use break in this case thank you!
[toc] | [prev] | [next] | [standalone]
| From | Christopher Welborn <cjwelborn@live.com> |
|---|---|
| Date | 2013-11-15 21:15 -0600 |
| Message-ID | <mailman.2700.1384574240.18130.python-list@python.org> |
| In reply to | #59582 |
Sorry about my previous post, gmane is being really slow. :( I wouldn't have posted if I knew the question was already answered. -- - Christopher Welborn <cjwelborn@live.com> http://welbornprod.com
[toc] | [prev] | [next] | [standalone]
| From | Christopher Welborn <cjwelborn@live.com> |
|---|---|
| Date | 2013-11-15 20:47 -0600 |
| Message-ID | <mailman.2699.1384574240.18130.python-list@python.org> |
| In reply to | #59582 |
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 <cjwelborn@live.com>
http://welbornprod.com
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web