problem with user confirmation

Bob Gailer bgailer at alum.rpi.edu
Fri Sep 19 11:18:24 EDT 2003


At 05:39 PM 9/18/2003, Duncan Smith wrote:


>"Matthew" <ruach at chpc.utah.edu> wrote in message
>news:ec1162c7.0309181523.2077b7e5 at posting.google.com...
> > I am have written a medium sized program and everything works fine
> > except for one piece. At a certain point in the program I want to do a
> > simple user confirmation, eg Are you sure ? y/n. The problem that I am
> > having is that after the user enters an answer and presses enter, the
> > program executes the rest of the code regardless of what the user
> > response was.
> >
> > Heres my code:
> >
> > def confirmAction(self, msg):
> > if not msg:
> > msg = 'Are you sure y/[n]> '
> > print msg
> > confirm = sys.stdin.readline()[:-1]
> > print 'confirm = \'%s\'' %(confirm)
> > if confirm == 'y' or 'Y' or 'Yes' or 'YES' or 'yes':
> > return 1
> > return 0
>
>[snip]
>
>'Y' evaluates to true.

To be more explicit:

given: confirm = 'y'
confirm == 'y' or 'Y' or 'Yes' or 'YES' or 'yes' evaluates to 1 (confirm == 
'y' returns 1)

but given: confirm = 'Y'
confirm == 'y' or 'Y' or 'Yes' or 'YES' or 'yes' evaluates to 'Y' (confirm 
== 'y' returns 0 which is ored with 'Y" which is true, hence the result is 'Y')

>Maybe try something like:
>
>if confirm in ['y', 'Y', 'Yes', 'YES', 'yes']:

Even better

if 'yes'.startswith(confirm.lower()): # covers y ye yes Y YE YES yES Yes etc.

Bob Gailer
bgailer at alum.rpi.edu
303 442 2625
-------------- next part --------------

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.506 / Virus Database: 303 - Release Date: 8/1/2003


More information about the Python-list mailing list