problem with user confirmation

M

Matthew

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

if confirmAction('Are you sure you want to DEACTIVATE this port?
y/[n]'):
print 'Deactivating
#Do some other stuff
else:
sys.exit(-1)

At this point in the program I can enter nothing, or y, or sdfasd, and
no matter what I do it prints 'Deactivating and executes the rest of
my code. I can't figure it out to save my life. I am sure that the
answer is very simple and that I have just been thinking to hard and
have over looked it.

Thanks for any help
-matthew
 
D

Duncan Smith

Matthew said:
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.

Maybe try something like:

if confirm in ['y', 'Y', 'Yes', 'YES', 'yes']:
etc.

Duncan
 
B

Bob Gailer

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

Matthew said:
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
(e-mail address removed)
303 442 2625
 
M

Matthew

That worked, thanks very much.

-matthew


Duncan Smith said:
Matthew said:
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.

Maybe try something like:

if confirm in ['y', 'Y', 'Yes', 'YES', 'yes']:
etc.

Duncan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top