Recognize a word from a list within a user input

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Netwatcher
    New Member
    • Sep 2008
    • 58

    Recognize a word from a list within a user input

    Hello, i am trying to make a program that recognizes a certain word from a list (as shown below),
    My main problem is-
    how do i make something that finds if the word from the given list is inside the user input, and by that i mean that the user input for 'hi' can be 'hi dude' and it doesn't has to be only 'hi'
    Code:
    import random
    from time import sleep
    hi='hi','HI','Hi','hI'
    hello='hello','Hello','HELLO'
    howdy='howdy','Howdy','HOWDY','HowDy'
    while 1:
        X=raw_input(random.choice(anything))
        if hi in X :
            print "<!--I'm  A  Comment-->"
            print random.choice(hello)
            break
        elif X in hello:
            print "<!--I'm A different Comment-->"
            print random.choice(hi)
            break
        elif X in howdy:
            print '<!--I am something completely different-->'
        elif X in curses:
            print random.choice(anticurse)
        sleep(0.5)
    This problem had bothered me for too long...
    Thank you in advance
    Last edited by Netwatcher; Sep 26 '08, 05:39 AM. Reason: grammer >>> I'm a grammer freak
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    It appears that you are comparing various combinations of upper and lower case letters of the same letters. Have you tried comparing the lower case word to the string in lower case?:[code=Python]>>> if 'hi' in 'ABCDEFGHiJKLM' .lower():
    ... print "It's in here."
    ...
    It's in here.
    >>> [/code]You could also do something like this:[code=Python]>>> hi = ('hi', 'HI', 'Hi', 'hI')
    >>> for word in hi:
    ... if word in 'ABCDEFGHiJKLM' :
    ... print '%s is in phrase %s' % (word, 'ABCDEFGHiJKLM' )
    ...
    Hi is in phrase ABCDEFGHiJKLM
    >>> [/code]

    Comment

    • Netwatcher
      New Member
      • Sep 2008
      • 58

      #3
      Finally,
      Thank you!
      I find the following code more useful for my purpose:

      Code:
      X=raw_input(random.choice(anything)).lower()
      i tried this weird combination and luckily for me, it worked :D
      solved me lots of problems and troubles

      Thanks again!

      Comment

      • Netwatcher
        New Member
        • Sep 2008
        • 58

        #4
        Awww... bummer...

        i got another problem!

        Code:
        curses: 'eggs','bacon','spam'
        def curse(X):
            for word in curses:
                if word in X:
                    print random.choice(anticurse)
                else:
                    print'<--ooʇ ʇuǝɯɯoɔ ɐ ɯ,ı--¡>'
                return(1)
          
        
        while 1:
            X=raw_input(random.choice(anything)).lower()
            if hi in X:
                print "<!--I'm  A  Comment-->"
                print hello
                #break
            elif hello in X:
                print "<!--I'm A different Comment-->"
                print hi
                #break
            else:
                callable(curse(X))
        When i run this code it only works for the first entry in the tuple (i tried with list and other groupings, but the problem remains)> if i enter 'eggs and ham'
        it recgonize it but if i enter 'bacon and ham' or just 'bacon' or 'spam' it doesn't.
        Last edited by Netwatcher; Sep 27 '08, 03:00 AM. Reason: oopsi

        Comment

        • boxfish
          Recognized Expert Contributor
          • Mar 2008
          • 469

          #5
          That makes sense. Did you know that a return statement instantly exits the function? You're checking the first word, then you get to the bottom of the loop, and you return. I don't know what the return is for, but it's definitely in the wrong place.

          Comment

          • Netwatcher
            New Member
            • Sep 2008
            • 58

            #6
            I do that, because, if i don't it prints '<--ooʇ ʇuǝɯɯoɔ ɐ ɯ,ı--¡>' 6 times in a row, with the return() it doesn't.
            i have no idea why (it seems weird for me too),
            but it works...

            Comment

            • boxfish
              Recognized Expert Contributor
              • Mar 2008
              • 469

              #7
              It doesn't do that over and over because the loop is not looping. It only makes it through the first iteration. Without the return, your program prints '<--ooʇ ʇuǝɯɯoɔ ɐ ɯ,ı--¡>' whenever a particular curse is not in X. It should do that over and over unless X is particularly curse-filled. If you don't wan't to see '<--ooʇ ʇuǝɯɯoɔ ɐ ɯ,ı--¡>', then get rid of the print statement, but you can't have the return.

              Hope this helps.

              Comment

              • Netwatcher
                New Member
                • Sep 2008
                • 58

                #8
                Oh 'blip' hehe, i didn't notice that i can achieve the same effect that i wanted to get by doing
                this-->
                Code:
                while 1:
                    X=raw_input(random.choice(anything)).lower()
                    if hi in X:
                        print "<!--I'm  A  Comment-->"
                        print hello
                        #break
                    elif hello in X:
                        print "<!--I'm A different Comment-->"
                        print hi
                    else:
                        for word in curses:
                            if word in X:
                                print random.choice(anticurse)
                        if X not in curses:
                            print '<--ooʇ ʇuǝɯɯoɔ ɐ ɯ,ı--¡>'
                still prints it 6 times though...
                it was my problem, sorry
                is there a way to Not let the 'else' fill the missing curse?

                Comment

                • Netwatcher
                  New Member
                  • Sep 2008
                  • 58

                  #9
                  never mind i got it figured...

                  Comment

                  Working...