Problem Checking for Tuple in Raw Input

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

    Problem Checking for Tuple in Raw Input

    Hi everyone i was just makeing myself a little spelling bee program and this problem came up that tells me the <string> needs to be left as an operand. I dont know how to fix this problem so any help you could give me would be much appreciated. Thank you
    Code:(python)


    [CODE=python]
    print 'please spell correctly'
    def loop():
    bad=('lol','tee hee','lawl','ro fl','lmao','lmf ao')
    response=raw_in put('please enter a laughter comment')
    if bad in response:
    print 'we dont use that language here'
    else:
    print 'i dont understand your gibberish'

    loop ()[/CODE]

    PS All my indentation is correct i just couldnt get this posting to indent my code block.
    Last edited by bartonc; Oct 20 '07, 07:57 PM. Reason: Added [CODE=python][/CODE] tags.
  • ilikepython
    Recognized Expert Contributor
    • Feb 2007
    • 844

    #2
    Originally posted by PythonNotSoGuru
    Hi everyone i was just makeing myself a little spelling bee program and this problem came up that tells me the <string> needs to be left as an operand. I dont know how to fix this problem so any help you could give me would be much appreciated. Thank you
    Code:(python)



    print 'please spell correctly'
    def loop():
    bad=('lol','tee hee','lawl','ro fl','lmao','lmf ao')
    response=raw_in put('please enter a laughter comment')
    if bad in response:
    print 'we dont use that language here'
    else:
    print 'i dont understand your gibberish'

    loop ()

    PS All my indentation is correct i just couldnt get this posting to indent my code block.
    Instead of "bad in response" try "response in bad".

    Comment

    • PythonNotSoGuru
      New Member
      • Oct 2007
      • 21

      #3
      Yeah sorry I tried that and that works exept that it will only work for one word answers. YOu cant have it find the word in a sentence for example. Any other ideas out there?

      Comment

      • ilikepython
        Recognized Expert Contributor
        • Feb 2007
        • 844

        #4
        Originally posted by PythonNotSoGuru
        Yeah sorry I tried that and that works exept that it will only work for one word answers. YOu cant have it find the word in a sentence for example. Any other ideas out there?
        You could try something like this:
        [code=python]
        bit = 0
        for word in bad:
        if word in response:
        bit = 1
        break

        if bit:
        print "Bad language"
        [/code]

        Comment

        • PythonNotSoGuru
          New Member
          • Oct 2007
          • 21

          #5
          Ok thank you for your help. I will definetly try using this code you gave. I was also wondering WHY python dosnt make this work properly when i have it like i did originally. It seems like the logic is sound. Thank you for any explanations you could give to further my understanding of Python. Any other suggestions beside the ones already given will be much appreciated. Thank you again.

          Comment

          • ilikepython
            Recognized Expert Contributor
            • Feb 2007
            • 844

            #6
            Originally posted by PythonNotSoGuru
            Ok thank you for your help. I will definetly try using this code you gave. I was also wondering WHY python dosnt make this work properly when i have it like i did originally. It seems like the logic is sound. Thank you for any explanations you could give to further my understanding of Python. Any other suggestions beside the ones already given will be much appreciated. Thank you again.
            The reason why it didn't work your original way, is because you were testing to see if a tuple (it doens't make a difference what it contains) is in a string. You need to test individually for every string in the tuple if it is found in the user input.

            Comment

            Working...