Harvey Fletcher Harvey Fletcher - 1 year ago 113
Python Question

Python check if any part of string in list

I have a list containing synonyms for the word 'Good' (this list here is shortened)

good_synonym = ['Good','good','Well','well']

And the program asks how the user is feeling

print = 'Hello, ' + name + ', How are you?'
status = raw_input('')

But sometimes, the user may respond to the question with "I am good" (or similar)

If the answer contains a word in the good synonym list, I want the program to reply

if status contains a word in good_synonym:
print ('That is good')
print ('That is not so good')

note that the first line is not real python language

But I don't know which phrase to use to do the action.

Answer Source

There are many ways you could try to do this. Since you are a beginner, let's just go for something that will work - efficiency should NOT be your first consideration.

status = status.split() # breaks response into words
if any(s in good_synonyms for s in status):
    print('That is good')

Of course it won't stop your program from acting as though "not good" is a reply deserving a happy answer, but this is a programming site.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download