3

I'm new to coding, and am wondering if there is a way to simplify this:

if "!" in name or "$" in name or "%" in name or "^" in name or "&" in name or "*" in name or "(" in name or ")" in name or "_" in name or "-" in name or "+" in name or "=" in name:
points = points + 1

Thank you

  • 7
    `if any(c in name for c in '!$%^'): ...`. (I didn't type out all the characters for brevity) – mgilson Nov 04 '17 at 20:55
  • 1
    @mgilson If you wanted brevity, you would've added those few chars instead of adding "(I didn't type out all the characters for brevity)" :-P – Stefan Pochmann Nov 04 '17 at 21:16
  • Here is a one-liner based on mgilson: `points += any(c in name for c in "!$%^&*()_-+=")`. This works because: If false: points += 0, True: Points +=1. Thanks to Stefan Pochmann too. – Anton vBR Nov 04 '17 at 21:27
  • @StefanPochmann -- Ahh, but you forget the amount of time it takes to cross-reference to make sure that I got them all. – mgilson Nov 05 '17 at 00:01

3 Answers3

3

You can use regex:

import re
if re.findall('[\!\$\%\^\&\*\_\-\+\=]', name):
   points += 1
Ajax1234
  • 69,937
  • 8
  • 61
  • 102
2
chars = set("!$%^&*()_-+=")

if any((i in chars) for i in string):
  points += 1
Alex Pshenko
  • 167
  • 8
1

You can write a simple function to achieve this.

def is_included(characters, sentence):
    for character in characters:
        if character in sentence:
            return True
    return False

As mgilson mention you can use any keyword too. However since you are new to programming I suggest you to learn the algorithms of keywords.