-3

I am trying to make a censor. Instead of doing

if curseword in typed or curseword2 in typed or curseword3 typed:
    print "No cursing! It's not nice!"

I want to make it so that I can have a list with all the words in it, and can check if those words are in the list. Note: if you use the "if any..." code, with a while loop, it has too much output to process.

GopherMan
  • 11
  • 4
Oughh
  • 302
  • 1
  • 2
  • 10
  • 3
    What does "Code must be compatible with while loops." mean, exactly? – SiHa Dec 15 '15 at 21:57
  • **@SiHa** What I mean is that if there is a for in that code, it will endlessly spew output and break the entire world. I need it to not have for loops or else that will happen. – Oughh Dec 15 '15 at 22:04
  • 1
    I'm sure that's because you implemented it incorrectly. – That1Guy Dec 15 '15 at 22:05
  • Possible duplicate of [How to check if a string contains an element from a list in Python](http://stackoverflow.com/q/6531482) – Bhargav Rao Dec 15 '15 at 22:06
  • You may get some false positives this way - but it's not as bad as the Clbuttic Mistake – John La Rooy Dec 15 '15 at 22:07
  • **@Bhargav Rao** I posted this question because the other question did NOT answer my question. I put this up because my code requires something without any for loops. By putting it on hold, there is now no way for me to get help. So... yeah. Good job. – Oughh Dec 15 '15 at 22:15
  • So you are saying a `for` loop will spew on endlessly but a `while` loop won't? Usually it's the other way round if anything. – John La Rooy Dec 15 '15 at 22:52
  • BTW, it's _not_ a good idea to use `input` as a variable name as that shadows the built-in `input` function, which is confusing to people reading your code and may lead to mysterious bugs. – PM 2Ring Dec 16 '15 at 08:48
  • @Oughh: You do realise that the answer you have marked as correct is exactly the same as the accepted answer in the duplicate that you say doesn't work? – SiHa Dec 16 '15 at 09:13
  • **@SiHa**Yes I do. The problem is that it's the least bad, not the most good. I need this code to work with while loops. If you test out that code with a while loop, you get the error "too much output to process". Also to PM my variables in the actual code are not called input. – Oughh Dec 16 '15 at 22:01

3 Answers3

14

You can use any plus a generator:

cursewords = ['javascript', 'php', 'windows']
if any(curseword in input for curseword in cursewords):
    print 'onoes'

Alternatively, for a bit more flexibility, a regex (if you want to do stuff like detect uppercase curse words as well):

if re.search(r'javascript|php|windows', input, re.IGNORECASE):
    print 'onoes'

(If you're new to regex, the Python docs have got a nice tutorial.)

If you just want to ignore case without messing with regexen, you can do that too:

# make sure these are all lowercase
cursewords = ['javascript', 'php', 'windows']
input_lower = input.lower()
if any(curseword in input_lower for curseword in cursewords):
    print 'onoes'
tckmn
  • 57,719
  • 27
  • 114
  • 156
  • 1
    About that regex way, use `'|'.join(cursewords)` then we don't need type these words from the list manually :) – Remi Guan Dec 15 '15 at 22:00
  • @KevinGuan Probably not a good idea—what if the "curse words" contain regex special chars? Better to just have a single regex, so you can also do stuff like `java(script)?`. – tckmn Dec 15 '15 at 22:01
  • Usually you should `re.escape` strings before `'|'.join`ing them into a regex. – John La Rooy Dec 15 '15 at 22:01
  • @Doorknob冰: Yeah `re.escape()` works as John said. So `re.search(re.escape('|'.join(cursewords)))`. – Remi Guan Dec 15 '15 at 22:02
  • 1
    @KevinGuan Nope, you want `'|'.join(map(re.escape, cursewords))`. Otherwise you're escaping the `|`s. – tckmn Dec 15 '15 at 22:03
  • @Doorknob冰 Yes. (checks - I did say "before"!) – John La Rooy Dec 15 '15 at 22:04
  • @Doorknob冰: Oops, forgot about that :P – Remi Guan Dec 15 '15 at 22:06
  • **@Doorknob冰** The problem with code like yours, though effective without a while loop, is that with a while loop that says 'while True:' a for loop will infinitely give output because of the while loop. How do I fix that? – Oughh Dec 15 '15 at 22:12
  • 2
    @Oughh I'm not sure what you're trying to say. This isn't an infinite loop. – tckmn Dec 15 '15 at 22:13
  • I am not so good at coding, but what happens is that because of my while loop, for some reason all for loops that I put in become infinite. – Oughh Dec 15 '15 at 22:18
  • 1
    @Oughh I... still have no idea what you're talking about. Perhaps that would be better as a separate question—include *all* of the code necessary to reproduce the issue. – tckmn Dec 15 '15 at 22:18
  • **@Doorknob**while True: if something is something: do something else: for blah in blahblah: if something is not something: print 'nope' << – Oughh Dec 15 '15 at 22:24
  • FWIW, `(curseword in input for curseword in cursewords)` is a _generator expression_. In Python, the term "comprehension" usually refers to a _list comprehension_, eg `[curseword in input for curseword in cursewords]`. Of course, passing a generator expression to `any` is much better than passing a list comprehension to `any` (or `all`), since the former can short-circuit, whereas the latter has to build the complete list before it can start testing. – PM 2Ring Dec 16 '15 at 08:46
  • @PM2Ring Ah, clearly I'm not up to speed with my Python jargon. Thanks for teaching me! – tckmn Dec 16 '15 at 12:16
1

Use a for loop on the input and check each word to see if it's in the list of cursewords.

cursewordList = ['a','b' ...]

for word in input:
    if word in cursewordList:
          print "No cursing! It's not nice!"
Jenny L
  • 111
  • 5
0

Using, filter built-in method as well:

>>>test = ['ONE', 'TWO', 'THREE', 'FOUR']
>>>input = 'TWO'

>>> if filter(lambda s: s in input, test):
    print 'OK'


OK
>>> input = 'FIVE'
>>> 
>>> if filter(lambda s: s in input, test):
    print 'OK'


>>> #nothing printed
Iron Fist
  • 10,739
  • 2
  • 18
  • 34