0
def parse_shot_success(string):
    """
    Determine if the shot was successful.
    Input:
        string: a string to be analyzed
    Returns:
        "scores" or "misses" or "not a shot" based on
        the shot success.
    """
    pp = re.compile("(scores|misses|blocks)")
    match = pp.search(string)
    if match.group(1) == "scores":
        return "scores"
    elif match.group(1) == ("blocks|misses"):
        return "misses"
    else:
        return "not a shot"

Hi, I want to to return "misses" or "scores" based on if the player scores or misses in the string so that parse_shot_success("Johnson blocks Lebron's shot") will return misses. I think i have to use for loop but i am not sure how i can incorporate it to my code. Can you help me thanks.

9
  • Why using regular expressions? Something like if 'scores' in string: return 'scores'; elif 'misses' in string: return 'misses'; else: return 'not a shot'. There's room for improvement, but that would work Commented Feb 5, 2016 at 13:03
  • By the way, in your re-based solution, I think you meant to write match.group(1) == "misses" instead of match.group(1) == ("scores|misses") Commented Feb 5, 2016 at 13:03
  • becuse I am trying to return "misses" even though there is "blocks" in sentence Commented Feb 5, 2016 at 13:09
  • sorry i edited it a bit it should be "blocks|misses" Commented Feb 5, 2016 at 13:22
  • Then you want match.group(1) in ('blocks', 'misses'). Still, I think regular expressions are overkill for this task Commented Feb 5, 2016 at 13:23

1 Answer 1

1

If you really want to go with regular expressions:

def parse_shot_success(string):
    pp = re.compile("(scores|misses|blocks)")
    match = pp.search(string)

    if not match:
        # https://docs.python.org/dev/library/re.html#re.search
        # "Return None if no position in the string matches the pattern"
        return "not a shot"
    elif match.group(1) == "scores":
        return "scores"
    elif match.group(1) in ("blocks", "misses"):
        return "misses"
    else:
        raise AssertionError

But this problem can be easily solved in other ways:

def parse_shot_success(string):
    if 'scores' in string:
        return 'scores'
    elif 'blocks' in string or 'misses' in string:
        return 'misses'
    else:
        return 'not a shot'

Or also:

def parse_shot_success(string):
    words = [
        # (word, return value)
        ('scores', 'scores'),
        ('blocks', 'misses'),
        ('misses', 'misses'),
    ]

    for word, result in words:
        if word in string:
            return result

    return 'not a shot'

There are a few problems that you have not considered:

  • What if I give you the string "SCORE" (uppercase)?
  • What if there's a player whose name is "blocksmith" (contains the word "blocks")? This is a case where using regular expressions may be the easiest way.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.