Using For Loops to check for tuples in raw input

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • PythonNotSoGuru
    New Member
    • Oct 2007
    • 21

    Using For Loops to check for tuples in raw input

    Hello everybody I have been trying to get around the problem of checking for a tuple in raw input and I just got a tip that kind of works but it only works for the second for loop. In other words when you input and of the 'close' words python returns 'thanx for chattin' like it should but if you input any of the 'bad' words it dosnt return anything. I have no idea what the problem is so any help would be much appreciated.
    Thank you.
    PS I dont know why ,my postings will never indent properly but i think i have them all right.
    [CODE=python]
    print 'hello'
    def loop():
    bit=0
    bad=('lol','law l','teehee','ro fl','lmao','lmf ao')
    close=('close', 'goodbye','shut up','be quiet')
    response=raw_in put()
    for word in bad:
    if word in response:
    bit=1
    if bit:
    print 'we dont use that language here'
    else:
    print 'i dont understand your gibberish'
    break
    break
    for word2 in close:
    if word2 in response:
    bit=1
    if bit:
    print 'thanx for chattin'
    else:
    print 'i dont understand your gibberish'
    break



    loop ()
    loop ()[/CODE]
    Last edited by bartonc; Oct 27 '07, 07:44 PM. Reason: Added [CODE=python][/CODE] tags.
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    Originally posted by PythonNotSoGuru
    Hello everybody I have been trying to get around the problem of checking for a tuple in raw input and I just got a tip that kind of works but it only works for the second for loop. In other words when you input and of the 'close' words python returns 'thanx for chattin' like it should but if you input any of the 'bad' words it dosnt return anything. I have no idea what the problem is so any help would be much appreciated.
    Thank you.
    PS I dont know why ,my postings will never indent properly but i think i have them all right.
    [CODE=python]
    print 'hello'
    def loop():
    bit=0
    bad=('lol','law l','teehee','ro fl','lmao','lmf ao')
    close=('close', 'goodbye','shut up','be quiet')
    response=raw_in put()
    for word in bad:
    if word in response:
    bit=1
    if bit:
    print 'we dont use that language here'
    else:
    print 'i dont understand your gibberish'
    break
    break
    for word2 in close:
    if word2 in response:
    bit=1
    if bit:
    print 'thanx for chattin'
    else:
    print 'i dont understand your gibberish'
    break



    loop ()
    loop ()[/CODE]
    The in operator can be used in a very cool way, here. It will simplify your logic greatly:[CODE=python]
    >>> bad=('lol','law l','teehee','ro fl','lmao','lmf ao')
    >>> close=('close', 'goodbye','shut up','be quiet')
    >>> def CheckInputInTup le(word):
    ... if word in bad:
    ... print 'we dont use that language here'
    ... elif word in close:
    ... print 'thanx for chattin'
    ... else:
    ... print 'i dont understand your gibberish'
    ...
    >>> def GetUserInput():
    ... word = ""
    ... while word.lower() not in close:
    ... word = raw_input("Let' s chat: ")
    ... CheckInputInTup le(word.lower() )
    ...
    >>> GetUserInput()[/CODE]Would "ROFL" also be considered bad?
    Last edited by bartonc; Oct 27 '07, 08:07 PM.

    Comment

    • PythonNotSoGuru
      New Member
      • Oct 2007
      • 21

      #3
      Ok cool now could you please explain this line by line to me because I am a bit confused as to how this works. Thank you so much for your input already.

      Comment

      • PythonNotSoGuru
        New Member
        • Oct 2007
        • 21

        #4
        Hi ok nevermind about that last comment I figured out what it does but now there it wont work for multiple word inputs such as "lol that was funny". When that is input it brings up "I dont understand your gibberish" becuase i can only check to see if the "bad" words are in a one word raw input. Any ways to fix this problem?
        thanks

        Comment

        • bvdet
          Recognized Expert Specialist
          • Oct 2006
          • 2851

          #5
          Originally posted by PythonNotSoGuru
          Hi ok nevermind about that last comment I figured out what it does but now there it wont work for multiple word inputs such as "lol that was funny". When that is input it brings up "I dont understand your gibberish" becuase i can only check to see if the "bad" words are in a one word raw input. Any ways to fix this problem?
          thanks
          I modified Barton's code for phrases instead of single words.[code=Python]bad = ('lol','lawl',' teehee','rofl', 'lmao','lmfao')
          close = ('close','goodb ye','shutup','b e quiet')

          def CheckInputInTup le(phrase):
          for word in phrase.split():
          if word in bad:
          return 1 #'we dont use that language here'
          elif word in close:
          return 0 #'thanx for chattin'
          return 2 # 'i dont understand your gibberish'

          def GetUserInput():
          while True:
          phrase = raw_input("Let' s chat: ")
          ans = CheckInputInTup le(phrase.lower ())
          if ans == 1:
          print 'we dont use that language here'
          elif not ans:
          print 'thanx for chattin'
          break
          else:
          print 'i dont understand your gibberish'

          GetUserInput()[/code]

          Comment

          Working...