Re: Trying To Catch Invalid User Input

2009-08-24 Thread Victor Subervi
That's nice. Thanks!
V

On Sun, Aug 23, 2009 at 5:02 PM, Dennis Lee Bieber wrote:

> On Sun, 23 Aug 2009 10:04:57 -0500, Victor Subervi
>  declaimed the following in
> gmane.comp.python.general:
>
> > Hi;
> > I have the following:
> >
> > style = raw_input('What style is this? (1 = short, 2 = long): ')
> > flag = 0
> > while flag == 0:
> >   if (style != 1) or (style != 2):
> > style = raw_input('There was a mistake. What style is this? (1 =
> short,
> > 2 = long): ')
> >   else:
> > flag = 1
> >
> Ugh... Unless you are using an archaic version of Python, the
> language has support for booleans...
>
> done = False
> while not done:
>...
>done = True
>
>
>You fail to convert the character input from raw_input into an
> integer for comparison... Oh, and you say the input is wrong if it is
> NOT 1 OR NOT 2... 1 is not 2, and 2 is not 1, so... even if you did
> convert to an integer, it would be rejected.
>
>Consider:
>
> -=-=-=-=-=-=-=-
> while True:
>charin = raw_input("What style is this? (1: short, 2: long): ")
>try:
>style = int(charin)
>except: #I know, should not use naked except clause
>print ("The input '%s' is not a valid integer value; try again"
>% charin)
>else:
>if style in (1, 2): break
>print ("The input value '%s' is not in the valid range; try
> again"
>   % style)
> -=-=-=-=-=-=-=-
> {Watch out for line wraps}
> Works in Python 2.5
>
> E:\UserData\Dennis Lee Bieber\My Documents>python Script1.py
> What style is this? (1: short, 2: long): ab1
> The input 'ab1' is not a valid integer value; try again
> What style is this? (1: short, 2: long): 1a
> The input '1a' is not a valid integer value; try again
> What style is this? (1: short, 2: long): 12
> The input value '12' is not in the valid range; try again
> What style is this? (1: short, 2: long): 2
>
> E:\UserData\Dennis Lee Bieber\My Documents>
>
> --
>Wulfraed Dennis Lee Bieber   KD6MOG
>wlfr...@ix.netcom.com   HTTP://wlfraed.home.netcom.com/
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Trying To Catch Invalid User Input

2009-08-23 Thread Victor Subervi
Really slick! Thanks!
V

On Sun, Aug 23, 2009 at 11:08 AM, Albert Hopkins wrote:

> On Sun, 2009-08-23 at 16:36 +0100, MRAB wrote:
> > Victor Subervi wrote:
> > > Hi;
> > > I have the following:
> > >
> > > style = raw_input('What style is this? (1 = short, 2 = long): ')
> > > flag = 0
> > > while flag == 0:
> > >   if (style != 1) or (style != 2):
> > > style = raw_input('There was a mistake. What style is this? (1 =
> > > short, 2 = long): ')
> > >   else:
> > > flag = 1
> > >
> > > I would think this would catch errors and permit valid values, but it
> > > doesn't. If I enter an erroneous value the first time, and the second
> > > time a good value, it doesn't break the loop. Why?
> > >
> > This is wrong:
> >
> >  (style != 1) or (style != 2)
> >
> > For example, if style is 1 (which should be a valid value):
> >
> >  (style != 1) or (style != 2)
> >   => (1 != 1) or (1 != 2)
> >   => Falseor True
> >   => True
> >
> > What you mean is:
> >
> >  (style != 1) and (style != 2)
>
> Or (perhaps) better:
>
> VALID_STYLES = {1: 'short', 2: 'long'}
>
> style = None
> while style not in VALID_STYLES:
>style = raw_input('What style is this? %s: ' %
>str(VALID_STYLES).replace(':', ' ='))
>
># also, raw_input() returns a string
>try:
>style = int(style)
>except ValueError:
>pass
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Trying To Catch Invalid User Input

2009-08-23 Thread Albert Hopkins
On Sun, 2009-08-23 at 16:36 +0100, MRAB wrote:
> Victor Subervi wrote:
> > Hi;
> > I have the following:
> > 
> > style = raw_input('What style is this? (1 = short, 2 = long): ')
> > flag = 0
> > while flag == 0:
> >   if (style != 1) or (style != 2):
> > style = raw_input('There was a mistake. What style is this? (1 = 
> > short, 2 = long): ')
> >   else:
> > flag = 1
> > 
> > I would think this would catch errors and permit valid values, but it 
> > doesn't. If I enter an erroneous value the first time, and the second 
> > time a good value, it doesn't break the loop. Why?
> > 
> This is wrong:
> 
>  (style != 1) or (style != 2)
> 
> For example, if style is 1 (which should be a valid value):
> 
>  (style != 1) or (style != 2)
>   => (1 != 1) or (1 != 2)
>   => Falseor True
>   => True
> 
> What you mean is:
> 
>  (style != 1) and (style != 2)

Or (perhaps) better:

VALID_STYLES = {1: 'short', 2: 'long'}

style = None
while style not in VALID_STYLES:
style = raw_input('What style is this? %s: ' %
str(VALID_STYLES).replace(':', ' ='))

# also, raw_input() returns a string
try:
style = int(style)
except ValueError:
pass


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Trying To Catch Invalid User Input

2009-08-23 Thread Gary Herron

Victor Subervi wrote:

Hi;
I have the following:

style = raw_input('What style is this? (1 = short, 2 = long): ')
flag = 0
while flag == 0:
  if (style != 1) or (style != 2):
style = raw_input('There was a mistake. What style is this? (1 = 
short, 2 = long): ')

  else:
flag = 1

I would think this would catch errors and permit valid values, but it 
doesn't. If I enter an erroneous value the first time, and the second 
time a good value, it doesn't break the loop. Why?

TIA,
Victor
First, raw_input will not return an integer, but rather characters, so 
the value of style will never be 1 or 2, but rather '1', or '2'.  

But even if you make your comparisons against  '1' and '2', this will 
not work since you've also made a simple logic error.  The conditional

 if (style != '1') or (style != '2'):
will always be True no matter what value  style has.  For instance
if style is '1', then the conditional evaluates like
 (False) or (True)
which evaluates to True. 


You want one of the following:
 if (style != '1') and (style != '2'):
or
 if not (style == '1' or style == '2'):
or
 if style == '1' or style == '2':
   flag = 1
 ...
or
 if style in ['1','2']:
   flag = 1
 ...
or better yet dispense with flag altogether
 while style not in ['1','2']:
   style = raw_input('There was a mistake ...

Gary Herron




--
http://mail.python.org/mailman/listinfo/python-list


Re: Trying To Catch Invalid User Input

2009-08-23 Thread MRAB

Victor Subervi wrote:

Hi;
I have the following:

style = raw_input('What style is this? (1 = short, 2 = long): ')
flag = 0
while flag == 0:
  if (style != 1) or (style != 2):
style = raw_input('There was a mistake. What style is this? (1 = 
short, 2 = long): ')

  else:
flag = 1

I would think this would catch errors and permit valid values, but it 
doesn't. If I enter an erroneous value the first time, and the second 
time a good value, it doesn't break the loop. Why?



This is wrong:

(style != 1) or (style != 2)

For example, if style is 1 (which should be a valid value):

(style != 1) or (style != 2)
 => (1 != 1) or (1 != 2)
 => Falseor True
 => True

What you mean is:

(style != 1) and (style != 2)
--
http://mail.python.org/mailman/listinfo/python-list