Help | Site Map
Connecting Tech Pros Worldwide
Reply
 
LinkBack Thread Tools
  #1  
Old September 26th, 2008, 06:37 AM
Member
 
Join Date: Sep 2008
Posts: 35
Post 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'
Expand|Select|Wrap|Line Numbers
    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; September 26th, 2008 at 06:39 AM. Reason: grammer >>> I'm a grammer freak
Reply
  #2  
Old September 26th, 2008, 02:57 PM
bvdet's Avatar
Expert
 
Join Date: Oct 2006
Location: Nashville, TN
Posts: 1,193
Default

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]
Reply
  #3  
Old September 26th, 2008, 11:57 PM
Member
 
Join Date: Sep 2008
Posts: 35
Talking

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

Expand|Select|Wrap|Line Numbers
    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!
Reply
  #4  
Old September 27th, 2008, 03:32 AM
Member
 
Join Date: Sep 2008
Posts: 35
Exclamation

Awww... bummer...

i got another problem!

Expand|Select|Wrap|Line Numbers
    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; September 27th, 2008 at 04:00 AM. Reason: oopsi
Reply
  #5  
Old September 27th, 2008, 03:49 AM
boxfish's Avatar
Needs Regular Fix
 
Join Date: Mar 2008
Location: ♩♫♬♪♩
Posts: 352
Default

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.
Reply
  #6  
Old September 27th, 2008, 04:02 AM
Member
 
Join Date: Sep 2008
Posts: 35
Exclamation

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...
Reply
  #7  
Old September 27th, 2008, 04:15 AM
boxfish's Avatar
Needs Regular Fix
 
Join Date: Mar 2008
Location: ♩♫♬♪♩
Posts: 352
Default

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.
Reply
  #8  
Old September 27th, 2008, 04:16 AM
Member
 
Join Date: Sep 2008
Posts: 35
Exclamation

Oh 'blip' hehe, i didn't notice that i can achieve the same effect that i wanted to get by doing
this-->
Expand|Select|Wrap|Line Numbers
    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?
Reply
  #9  
Old September 28th, 2008, 04:00 AM
Member
 
Join Date: Sep 2008
Posts: 35
Default

never mind i got it figured...
Reply
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles