Problem I have with a while loop/boolean/or

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • israphelr@googlemail.com

    #1

    Problem I have with a while loop/boolean/or

    Hi all.

    I have a problem with some code :(

    -----------

    hint = raw_input("\nAr e you stuck? y/n: ")
    hint = hint.lower()

    while (hint != 'n') or (hint != 'y'):
    hint = raw_input("Plea se specify a valid choice: ")

    ---------------------------------------------

    so everytime I run the program, and enter my choice as y or n, I'm
    told to 'Please Specify a valid Choice Again' and can't get out of the
    loop.



    -----------

    hint = raw_input("\nAr e you stuck? y/n: ")
    hint = hint.lower()

    while (hint != 'n'):
    hint = raw_input("Plea se specify a valid choice: ")

    ---------------------------------------------

    As for here when I enter n, I can leave the while loop.

    Anyway I can't put my finger on this, so I'd be real grateful if
    someone could tell me what I've done wrong. Thanks.
    (I have a guy feeling it's something really silly I'm overlooking...)

  • Paul Rubin

    #2
    Re: Problem I have with a while loop/boolean/or

    "israphelr@goog lemail.com" <israphelr@goog lemail.comwrite s:
    while (hint != 'n') or (hint != 'y'):
    hint = raw_input("Plea se specify a valid choice: ")
    >
    ---------------------------------------------
    >
    so everytime I run the program, and enter my choice as y or n, I'm
    told to 'Please Specify a valid Choice Again' and can't get out of the
    loop.
    Suppose your hint is 'y'. Ask yourself these two questions:

    1. Is (hint != 'n') true?
    2. Is (hint != 'y') true?

    Now ask yourself what the condition in your while loop says.

    Comment

    • Larry Bates

      #3
      Re: Problem I have with a while loop/boolean/or

      israphelr@googl email.com wrote:
      Hi all.
      >
      I have a problem with some code :(
      >
      -----------
      >
      hint = raw_input("\nAr e you stuck? y/n: ")
      hint = hint.lower()
      >
      while (hint != 'n') or (hint != 'y'):
      hint = raw_input("Plea se specify a valid choice: ")
      >
      ---------------------------------------------
      >
      so everytime I run the program, and enter my choice as y or n, I'm
      told to 'Please Specify a valid Choice Again' and can't get out of the
      loop.
      >
      >
      >
      -----------
      >
      hint = raw_input("\nAr e you stuck? y/n: ")
      hint = hint.lower()
      >
      while (hint != 'n'):
      hint = raw_input("Plea se specify a valid choice: ")
      >
      ---------------------------------------------
      >
      As for here when I enter n, I can leave the while loop.
      >
      Anyway I can't put my finger on this, so I'd be real grateful if
      someone could tell me what I've done wrong. Thanks.
      (I have a guy feeling it's something really silly I'm overlooking...)
      >
      Your if test is wrong.

      If hint='n'

      hint !='n' is False
      hint !='y' is True

      False or True equals True

      I think you want something like:

      hint = raw_input("\nAr e you stuck? y/n: ")
      hint = hint.lower()

      while hint not in ['y', 'n']:
      hint = raw_input("Plea se specify a valid choice: ")

      -Larry

      Comment

      • israphelr@googlemail.com

        #4
        Re: Problem I have with a while loop/boolean/or

        On Mar 13, 10:43 pm, Paul Rubin <http://phr...@NOSPAM.i nvalidwrote:
        "israph...@goog lemail.com" <israph...@goog lemail.comwrite s:
        while (hint != 'n') or (hint != 'y'):
        hint = raw_input("Plea se specify a valid choice: ")
        >
        ---------------------------------------------
        >
        so everytime I run the program, and enter my choice as y or n, I'm
        told to 'Please Specify a valid Choice Again' and can't get out of the
        loop.
        >
        Suppose your hint is 'y'. Ask yourself these two questions:
        >
        1. Is (hint != 'n') true?
        2. Is (hint != 'y') true?
        >
        Now ask yourself what the condition in your while loop says.
        Thanks.

        *puts my head under a white sheet* =/


        Comment

        • israphelr@googlemail.com

          #5
          Re: Problem I have with a while loop/boolean/or

          On Mar 13, 10:50 pm, Larry Bates <lba...@websafe .comwrote:
          israph...@googl email.com wrote:
          Hi all.
          >
          I have a problem with some code :(
          >
          -----------
          >
          hint = raw_input("\nAr e you stuck? y/n: ")
          hint = hint.lower()
          >
          while (hint != 'n') or (hint != 'y'):
          hint = raw_input("Plea se specify a valid choice: ")
          >
          ---------------------------------------------
          >
          so everytime I run the program, and enter my choice as y or n, I'm
          told to 'Please Specify a valid Choice Again' and can't get out of the
          loop.
          >
          -----------
          >
          hint = raw_input("\nAr e you stuck? y/n: ")
          hint = hint.lower()
          >
          while (hint != 'n'):
          hint = raw_input("Plea se specify a valid choice: ")
          >
          ---------------------------------------------
          >
          As for here when I enter n, I can leave the while loop.
          >
          Anyway I can't put my finger on this, so I'd be real grateful if
          someone could tell me what I've done wrong. Thanks.
          (I have a guy feeling it's something really silly I'm overlooking...)
          >
          Your if test is wrong.
          >
          If hint='n'
          >
          hint !='n' is False
          hint !='y' is True
          >
          False or True equals True
          >
          I think you want something like:
          >
          hint = raw_input("\nAr e you stuck? y/n: ")
          hint = hint.lower()
          >
          while hint not in ['y', 'n']:
          hint = raw_input("Plea se specify a valid choice: ")
          >
          -Larry
          Thanks Larry.

          Comment

          • John McMonagle

            #6
            Re: Problem I have with a while loop/boolean/or

            israphelr@googl email.com wrote:
            Hi all.
            >
            I have a problem with some code :(
            >
            -----------
            >
            hint = raw_input("\nAr e you stuck? y/n: ")
            hint = hint.lower()
            >
            while (hint != 'n') or (hint != 'y'):
            hint = raw_input("Plea se specify a valid choice: ")
            >
            ---------------------------------------------
            >
            so everytime I run the program, and enter my choice as y or n, I'm
            told to 'Please Specify a valid Choice Again' and can't get out of the
            loop.
            >
            >
            >
            -----------
            >
            hint = raw_input("\nAr e you stuck? y/n: ")
            hint = hint.lower()
            >
            while (hint != 'n'):
            hint = raw_input("Plea se specify a valid choice: ")
            >
            ---------------------------------------------
            >
            As for here when I enter n, I can leave the while loop.
            >
            Anyway I can't put my finger on this, so I'd be real grateful if
            someone could tell me what I've done wrong. Thanks.
            (I have a guy feeling it's something really silly I'm overlooking...)
            Try it a different way:

            while True:
            hint = raw_input("\nAr e you stuck? y/n: ")
            hint = hint.lower()
            if hint != 'y' and hint != 'n':
            print "Please answer y or n"
            continue
            else:
            break
            if hint == 'y':
            do_your_hint_st uff()

            >

            Comment

            • Bart Willems

              #7
              Re: Problem I have with a while loop/boolean/or

              John McMonagle wrote:
              Try it a different way:
              >
              while True:
              hint = raw_input("\nAr e you stuck? y/n: ")
              hint = hint.lower()
              if hint != 'y' and hint != 'n':
              print "Please answer y or n"
              continue
              else:
              break
              if hint == 'y':
              do_your_hint_st uff()

              I always try to stay away from 'negative' operators if possible, to
              improve readability:

              while True:
              hint = raw_input('\nAr e you stuck? y/n: ').lower()
              if hint = 'y' or hint = 'n':
              break
              else:
              print 'Please answer yes or no'

              Comment

              • hg

                #8
                Re: Problem I have with a while loop/boolean/or

                israphelr@googl email.com wrote:
                Hi all.
                >
                I have a problem with some code :(
                >
                -----------
                >
                hint = raw_input("\nAr e you stuck? y/n: ")
                hint = hint.lower()
                >
                while (hint != 'n') or (hint != 'y'):
                hint = raw_input("Plea se specify a valid choice: ")
                >
                ---------------------------------------------
                >
                so everytime I run the program, and enter my choice as y or n, I'm
                told to 'Please Specify a valid Choice Again' and can't get out of the
                loop.
                >
                >
                >
                -----------
                >
                hint = raw_input("\nAr e you stuck? y/n: ")
                hint = hint.lower()
                >
                while (hint != 'n'):
                hint = raw_input("Plea se specify a valid choice: ")
                >
                ---------------------------------------------
                >
                As for here when I enter n, I can leave the while loop.
                >
                Anyway I can't put my finger on this, so I'd be real grateful if
                someone could tell me what I've done wrong. Thanks.
                (I have a guy feeling it's something really silly I'm overlooking...)
                Hi,

                Whatever hint is:
                (hint != 'n') or (hint != 'y')
                is always going to be true

                as it cannot really be both at the same time

                use "and" instead of "or"

                hg






                Comment

                • hg

                  #9
                  Re: Problem I have with a while loop/boolean/or

                  hg wrote:
                  israphelr@googl email.com wrote:
                  >
                  >Hi all.
                  >>
                  >I have a problem with some code :(
                  >>
                  >-----------
                  >>
                  >hint = raw_input("\nAr e you stuck? y/n: ")
                  >hint = hint.lower()
                  >>
                  >while (hint != 'n') or (hint != 'y'):
                  > hint = raw_input("Plea se specify a valid choice: ")
                  >>
                  >---------------------------------------------
                  >>
                  >so everytime I run the program, and enter my choice as y or n, I'm
                  >told to 'Please Specify a valid Choice Again' and can't get out of the
                  >loop.
                  >>
                  >>
                  >>
                  >-----------
                  >>
                  >hint = raw_input("\nAr e you stuck? y/n: ")
                  >hint = hint.lower()
                  >>
                  >while (hint != 'n'):
                  > hint = raw_input("Plea se specify a valid choice: ")
                  >>
                  >---------------------------------------------
                  >>
                  >As for here when I enter n, I can leave the while loop.
                  >>
                  >Anyway I can't put my finger on this, so I'd be real grateful if
                  >someone could tell me what I've done wrong. Thanks.
                  >(I have a guy feeling it's something really silly I'm overlooking...)
                  >
                  Hi,
                  >
                  Whatever hint is:
                  (hint != 'n') or (hint != 'y')
                  is always going to be true
                  >
                  as it cannot really be both at the same time
                  >
                  use "and" instead of "or"
                  >
                  hg

                  oops, meant to answer to the main post

                  Comment

                  • Scott David Daniels

                    #10
                    Re: Problem I have with a while loop/boolean/or

                    Bart Willems wrote:
                    ...
                    I always try to stay away from 'negative' operators if possible, to
                    improve readability:
                    >
                    while True:
                    hint = raw_input('\nAr e you stuck? y/n: ').lower()
                    if hint = 'y' or hint = 'n':
                    break
                    else:
                    print 'Please answer yes or no'
                    And if you really want to see what is going wrong, replace that last by:
                    print 'Please answer yes or no (not %r):' % hint

                    --
                    --Scott David Daniels
                    scott.daniels@a cm.org

                    Comment

                    • Steve Holden

                      #11
                      Re: Problem I have with a while loop/boolean/or

                      Scott David Daniels wrote:
                      Bart Willems wrote:
                      >...
                      >I always try to stay away from 'negative' operators if possible, to
                      >improve readability:
                      >>
                      >while True:
                      > hint = raw_input('\nAr e you stuck? y/n: ').lower()
                      > if hint = 'y' or hint = 'n':
                      > break
                      > else:
                      > print 'Please answer yes or no'
                      >
                      And if you really want to see what is going wrong, replace that last by:
                      print 'Please answer yes or no (not %r):' % hint
                      >
                      Better still, use

                      Please answer y or n

                      since those are the values you are testing for ...

                      regards
                      Steve
                      --
                      Steve Holden +44 150 684 7255 +1 800 494 3119
                      Holden Web LLC/Ltd http://www.holdenweb.com
                      Skype: holdenweb http://del.icio.us/steve.holden
                      Blog of Note: http://holdenweb.blogspot.com
                      See you at PyCon? http://us.pycon.org/TX2007

                      Comment

                      • gslindstrom@gmail.com

                        #12
                        Re: Problem I have with a while loop/boolean/or

                        On Mar 13, 5:34 pm, "israph...@goog lemail.com"
                        <israph...@goog lemail.comwrote :
                        Hi all.
                        >
                        I have a problem with some code :(
                        >
                        -----------
                        >
                        hint = raw_input("\nAr e you stuck? y/n: ")
                        hint = hint.lower()
                        >
                        while (hint != 'n') or (hint != 'y'):
                        hint = raw_input("Plea se specify a valid choice: ")
                        >
                        I'd even make it a little more bullet-proof by using lower()

                        while hint.lower() not in ('y','n'):
                        stuff

                        That way if the user types in 'Y' on 'N' it will still work. I've
                        even gone further on occasion

                        while hint.lower()[0] not in ('y','n'):
                        stuff

                        so if they type 'Yes' or 'No' you're still good.

                        --greg

                        Comment

                        • Fabio FZero

                          #13
                          Re: Problem I have with a while loop/boolean/or

                          On Mar 13, 7:34 pm, "israph...@goog lemail.com"
                          <israph...@goog lemail.comwrote :
                          Hi all.
                          >
                          I have a problem with some code :(
                          >
                          -----------
                          >
                          hint = raw_input("\nAr e you stuck? y/n: ")
                          hint = hint.lower()
                          >
                          while (hint != 'n') or (hint != 'y'):
                          hint = raw_input("Plea se specify a valid choice: ")
                          >
                          ---------------------------------------------
                          >
                          so everytime I run the program, and enter my choice as y or n, I'm
                          told to 'Please Specify a valid Choice Again' and can't get out of the
                          loop.
                          Why don't you try this instead:

                          hint = raw_input("\nAr e you stuck? y/n: ")
                          hint = hint.lower()

                          while not hint in 'yn':
                          hint = raw_input("Plea se specify a valid choice: ")

                          []s
                          FZero

                          Comment

                          • Gabriel Genellina

                            #14
                            Re: Problem I have with a while loop/boolean/or

                            En Wed, 14 Mar 2007 15:34:20 -0300, Fabio FZero <fabio.fzero@gm ail.com>
                            escribió:
                            Why don't you try this instead:
                            >
                            hint = raw_input("\nAr e you stuck? y/n: ")
                            hint = hint.lower()
                            >
                            while not hint in 'yn':
                            hint = raw_input("Plea se specify a valid choice: ")
                            Dangerous: what if the user types 'yn'?
                            `hint in list('yn')` may be better, if you don't like the previous answers.

                            --
                            Gabriel Genellina

                            Comment

                            Working...