problem with user confirmation

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Matthew

    problem with user confirmation

    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(s elf, msg):
    if not msg:
    msg = 'Are you sure y/[n]> '
    print msg
    confirm = sys.stdin.readl ine()[:-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
  • Duncan Smith

    #2
    Re: problem with user confirmation


    "Matthew" <ruach@chpc.uta h.edu> wrote in message
    news:ec1162c7.0 309181523.2077b 7e5@posting.goo gle.com...[color=blue]
    > 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(s elf, msg):
    > if not msg:
    > msg = 'Are you sure y/[n]> '
    > print msg
    > confirm = sys.stdin.readl ine()[:-1]
    > print 'confirm = \'%s\'' %(confirm)
    > if confirm == 'y' or 'Y' or 'Yes' or 'YES' or 'yes':
    > return 1
    > return 0[/color]

    [snip]

    'Y' evaluates to true.

    Maybe try something like:

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

    Duncan


    Comment

    • Bob Gailer

      #3
      Re: problem with user confirmation

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

      [color=blue]
      >"Matthew" <ruach@chpc.uta h.edu> wrote in message
      >news:ec1162c7. 0309181523.2077 b7e5@posting.go ogle.com...[color=green]
      > > 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(s elf, msg):
      > > if not msg:
      > > msg = 'Are you sure y/[n]> '
      > > print msg
      > > confirm = sys.stdin.readl ine()[:-1]
      > > print 'confirm = \'%s\'' %(confirm)
      > > if confirm == 'y' or 'Y' or 'Yes' or 'YES' or 'yes':
      > > return 1
      > > return 0[/color]
      >
      >[snip]
      >
      >'Y' evaluates to true.[/color]

      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')
      [color=blue]
      >Maybe try something like:
      >
      >if confirm in ['y', 'Y', 'Yes', 'YES', 'yes']:[/color]

      Even better

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

      Bob Gailer
      bgailer@alum.rp i.edu
      303 442 2625


      ---
      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

      Comment

      • Matthew

        #4
        Re: problem with user confirmation

        That worked, thanks very much.

        -matthew


        "Duncan Smith" <buzzard@urubu. freeserve.co.uk > wrote in message news:<bkdfg7$lg a$1@news6.svr.p ol.co.uk>...[color=blue]
        > "Matthew" <ruach@chpc.uta h.edu> wrote in message
        > news:ec1162c7.0 309181523.2077b 7e5@posting.goo gle.com...[color=green]
        > > 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(s elf, msg):
        > > if not msg:
        > > msg = 'Are you sure y/[n]> '
        > > print msg
        > > confirm = sys.stdin.readl ine()[:-1]
        > > print 'confirm = \'%s\'' %(confirm)
        > > if confirm == 'y' or 'Y' or 'Yes' or 'YES' or 'yes':
        > > return 1
        > > return 0[/color]
        >
        > [snip]
        >
        > 'Y' evaluates to true.
        >
        > Maybe try something like:
        >
        > if confirm in ['y', 'Y', 'Yes', 'YES', 'yes']:
        > etc.
        >
        > Duncan[/color]

        Comment

        Working...