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.
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 workmatch.group(1) == "misses"instead ofmatch.group(1) == ("scores|misses")match.group(1) in ('blocks', 'misses'). Still, I think regular expressions are overkill for this task