Source

PythonDojo / BowlingWithAVengeance / bowling.py

STRIKE = 10
SPARE = '/'

def parse_roll(roll):
    if roll == '/':
        return SPARE
    return STRIKE
    
def frame_score(frame):
    return sum(frame)
    
def is_strike(frame):
    if frame[0]==10:
        return True
    return False

def is_spare(frame):
    return frame_score(frame) == 10 and not is_strike(frame)
    
def get_score(frames):
    score = 0
    
    for index, frame in enumerate(frames):
        score += frame_score(frame)
        if index < len(frames) - 1:
            # if this is not the last frame, we might want to
            # look at future frames for additional scores
            next_frame = frames[index+1]
            if is_strike(frame):
                score += frame_score(next_frame)
                if is_strike(next_frame):
                    next_next_frame = frames[index+2]
                    score += next_next_frame[0]
            elif is_spare(frame):
                score += 2
    return score