While loop with "or"? Please help!

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • wd.jonsson@gmail.com

    #1

    While loop with "or"? Please help!

    Hmm, my while loop with "or" doesn't seem to work as I want it to...
    How do I tell the while loop to only accept "Y" or "y" or "N" or "n"
    input from the str(raw_input)?

    Thank's in advance!

    Snippet of code:

    import os

    def buildfinder():
    os.system("CLS" )
    GameRoot = os.getenv("GAME _ROOT") + "\\"

    print "Do you want to use " + GameRoot + " as your source
    directory?"
    usr = str(raw_input(' Y/N: '))
    return usr

    #Runs the buildfinder function
    usrinp = buildfinder()

    def buildwhiler():

    while usrinp != "y" or "Y" or "N" or "n": <<<<----PROBLEM
    print "Enter Y or N!"
    usr = str(raw_input(' Y/N: '))
    else:
    code continues

  • Ravi Teja

    #2
    Re: While loop with &quot;or&quo t;? Please help!

    while usrinp != "y" or "Y" or "N" or "n": <<<<----PROBLEM
    Correct way:
    while usrinp != "y" or usrinp != "Y" or usrinp != "N" or usrinp != "n":
    There has to be a boolean evaluation on both sides of or.

    Or in this case:
    while usrinp not in ['Y', 'y', 'N', 'n']:

    Ravi Teja.

    Comment

    • Bruno Desthuilliers

      #3
      Re: While loop with &quot;or&quo t;? Please help!

      wd.jonsson@gmai l.com a écrit :
      Hmm, my while loop with "or" doesn't seem to work as I want it to...
      How do I tell the while loop to only accept "Y" or "y" or "N" or "n"
      input from the str(raw_input)?
      >
      Thank's in advance!
      >
      Snippet of code:
      >
      import os
      >
      def buildfinder():
      os.system("CLS" )
      GameRoot = os.getenv("GAME _ROOT") + "\\"
      >
      print "Do you want to use " + GameRoot + " as your source
      directory?"
      usr = str(raw_input(' Y/N: '))
      return usr
      >
      #Runs the buildfinder function
      usrinp = buildfinder()
      >
      def buildwhiler():
      >
      while usrinp != "y" or "Y" or "N" or "n": <<<<----PROBLEM
      Yes, obviously.

      The above reads as:
      while (usrinp != "y") or ("Y") or ("N") or ("n"):

      What you want is something like:
      while usrinp != "y" and usrinp != "Y" \
      and usrinp != "n" and usrinp != "N" :

      which is better written as
      while not (usrinp == "y" or usrinp == "Y" \
      or usrinp == "n" or usrinp == "N") :

      which can be simplified using str.lower:
      while not (usrinp.lower() == "y" or usrinp.lower() == "n"):

      and simplified again thanks to Python 'in' operator:
      while not usrinp.lower() in "yn":

      print "Enter Y or N!"
      usr = str(raw_input(' Y/N: '))
      else:
      code continues

      While we're at it, there are other problems in your code. One of them is
      that buildwhiler() will go in an infinite loop if the usr first typed
      anything else than "y", "Y", "n" or "N". I let you try to find out why,
      and just give you two hints :
      - globals are Bad(tm)
      - don't mix application logic with user-interface logic (ie: a function
      should only deal with one or the other, not mix both).

      HTH

      Comment

      • Paul Rubin

        #4
        Re: While loop with &quot;or&quo t;? Please help!

        wd.jonsson@gmai l.com writes:
        while usrinp != "y" or "Y" or "N" or "n": <<<<----PROBLEM
        while userinp not in 'yYnN':
        ...

        Or maybe more generally:

        while userinp.lower() not in 'yn': # case independent test
        ...

        Comment

        • Peter Otten

          #5
          Re: While loop with &quot;or&quo t;? Please help!

          Bruno Desthuilliers wrote:
          and simplified again thanks to Python 'in' operator:
          while not usrinp.lower() in "yn":
          But note that 'in' performs a substring search and therefore "yn" and ""
          would be accepted as valid answers, too.

          Peter

          Comment

          • Bruno Desthuilliers

            #6
            Re: While loop with &quot;or&quo t;? Please help!

            Peter Otten a écrit :
            Bruno Desthuilliers wrote:
            >
            >and simplified again thanks to Python 'in' operator:
            >while not usrinp.lower() in "yn":
            >
            But note that 'in' performs a substring search and therefore "yn" and ""
            would be accepted as valid answers, too.
            Mmm, right. Thanks for the correction.

            =>
            while not usrinp.lower() in ['y', 'n']:

            Comment

            • Tim Williams

              #7
              Re: While loop with &quot;or&quo t;? Please help!

              On 25/01/07, Bruno Desthuilliers <bruno.desthuil liers@websitebu ro.comwrote:
              Peter Otten a écrit :
              Bruno Desthuilliers wrote:
              and simplified again thanks to Python 'in' operator:
              while not usrinp.lower() in "yn":
              But note that 'in' performs a substring search and therefore "yn" and ""
              would be accepted as valid answers, too.
              >
              Mmm, right. Thanks for the correction.
              >
              =>
              while not usrinp.lower() in ['y', 'n']:
              or better still

              while usrinp.lower() not in ['y', 'n']:

              :):)


              --

              Tim Williams

              Comment

              • Paul Rubin

                #8
                Re: While loop with &quot;or&quo t;? Please help!

                Peter Otten <__peter__@web. dewrites:
                while not usrinp.lower() in "yn":
                >
                But note that 'in' performs a substring search and therefore "yn" and ""
                would be accepted as valid answers, too.
                Oh right, that's a recent change to the language, I think. OK:

                while not usrinp.lower() in list("yn"): ...

                Comment

                • wittempj@hotmail.com

                  #9
                  Re: While loop with &quot;or&quo t;? Please help!



                  On Jan 25, 11:26 am, wd.jons...@gmai l.com wrote:
                  Hmm, my while loop with "or" doesn't seem to work as I want it to...
                  How do I tell the while loop to only accept "Y" or "y" or "N" or "n"
                  input from the str(raw_input)?
                  >
                  Thank's in advance!
                  >
                  Snippet of code:
                  >
                  import os
                  >
                  def buildfinder():
                  os.system("CLS" )
                  GameRoot = os.getenv("GAME _ROOT") + "\\"
                  >
                  print "Do you want to use " + GameRoot + " as your source
                  directory?"
                  usr = str(raw_input(' Y/N: '))
                  return usr
                  >
                  #Runs the buildfinder function
                  usrinp = buildfinder()
                  >
                  def buildwhiler():
                  >
                  while usrinp != "y" or "Y" or "N" or "n": <<<<----PROBLEM
                  print "Enter Y or N!"
                  usr = str(raw_input(' Y/N: '))
                  else:
                  code continues
                  also note that your naming of variables contains two different names
                  for one variable...

                  better

                  usrinp = ''
                  while usrinp.lower() not in ('y', 'n'):
                  usrinp = raw_input('Y/N: ') # and not usr....

                  Comment

                  • Bruno Desthuilliers

                    #10
                    Re: While loop with &quot;or&quo t;? Please help!

                    wittempj@hotmai l.com a écrit :
                    >
                    On Jan 25, 11:26 am, wd.jons...@gmai l.com wrote:
                    (snip)
                    >#Runs the buildfinder function
                    >usrinp = buildfinder()
                    >>
                    >def buildwhiler():
                    >>
                    > while usrinp != "y" or "Y" or "N" or "n": <<<<----PROBLEM
                    > print "Enter Y or N!"
                    > usr = str(raw_input(' Y/N: '))
                    > else:
                    > code continues
                    >
                    also note that your naming of variables contains two different names
                    for one variable...
                    Actually, usrinp and usr are really two different vars - the first one
                    being global...

                    And FWIW, it's one of the "other errors" I mentionned earlier <g>

                    Comment

                    • Daniel

                      #11
                      Re: While loop with &quot;or&quo t;? Please help!

                      2007-01-25 11:26:09
                      wd.jonsson@gmai l.com wrote in message
                      <1169720769.490 434.91650@j27g2 000cwj.googlegr oups.com>
                      Hmm, my while loop with "or" doesn't seem to work as I want it to...
                      How do I tell the while loop to only accept "Y" or "y" or "N" or
                      "n"
                      input from the str(raw_input)?
                      >
                      Thank's in advance!
                      >
                      Snippet of code:
                      >
                      import os
                      >
                      def buildfinder():
                      os.system("CLS" )
                      GameRoot = os.getenv("GAME _ROOT") + "\\"
                      >
                      print "Do you want to use " + GameRoot + " as your source
                      directory?"
                      usr = str(raw_input(' Y/N: '))
                      return usr
                      >
                      #Runs the buildfinder function
                      usrinp = buildfinder()
                      >
                      def buildwhiler():
                      >
                      while usrinp != "y" or "Y" or "N" or "n": <<<<----PROBLEM
                      print "Enter Y or N!"
                      usr = str(raw_input(' Y/N: '))
                      else:
                      code continues
                      Thanks for the help everyone! :)
                      This forum rules!

                      Comment

                      Working...