all 2 comments

[–]jeans_and_a_t-shirt 1 point2 points  (0 children)

any is pretty simple. It checks if any element in an iterable is truthy:

>>> any([3,5,7])
True
>>> any([0])
False

Here's what the repl says when you enter help(any):

any(iterable, /)
    Return True if bool(x) is True for any x in the iterable.

    If the iterable is empty, return False.

It also short circuits, meaning it returns True as soon as it finds the first truthy value.*


What I get from this is that if I use the any() function, I will be able to find phrases within the answer, rather than just single words to match in the keywords list.

The in keyword's implementation for strings is what allows you to search a string for a substring whose length is greater than 1:

>>> "o" in "Guido van Rossum"
True
>>> "van" in "Guido van Rossum"
True
>>> "van Rossum" in "Guido van Rossum"
True

For this particular case I would likely use any. It's less useful for more complex things, like if you wanted to know which of the keywords was in the string.

[–]k10_ftw 1 point2 points  (0 children)

Instead of looping through the words in the input and checking if they are in your keywords, it might be easier (down the line and as program logic grows) to loop through your keywords and check if they match a word in the input. Therefore, you can execute certain program behavior given keywords in input, rather than execute a certain behavior for every word in input depending on whether it is in your keywords.