id/email
password
forgot password | create account
about | help | code help+videos | done | prefs
CodingBat code practice

 

david.white@denison.edu cs111fall2019 > bowling
prev  |  next  |  chance

A bowling game consists of ten frames. In each frame you get up to two balls to attempt to knock down the 10 pins. If you knock down all 10 pins on the first ball, that's called a strike; congratulations, sit down, you're done for the frame, and you only used one ball. However, the scoring for the frame involves more than just the one ball you used: you get 10 points for knocking down the 10 pins, plus whatever score you get on the next two balls (those balls will count both in this frame and the next). If you don't get all the pins on your first ball, you use a second ball; if that knocks down the remaining pins it is called a spare. The score for a spare is 10 for the pins you knocked down with the two balls, plus whatever you score on your next ball (which will also be counted in the next frame). Finally, if you get less than 10 pins down on the two balls, that's called an open frame; you score the total of the two balls. Some examples: if your first three balls are [10, 3, 4], then the 10 is a strike; score 10+3+4 = 17 for the first frame, and 3+4 = 7 on the second frame. If you roll a [7, 3, 5, 4], then the 7-3 is a spare, score 7+3+5 = 15 in the first frame, and the 5+4 = 9 on the second frame. The score for the game is the sum of the scores for the ten frames. One exception to these rules: if you get a spare or strike in the tenth frame, you roll three balls in that frame and score their total -- that is the only case where you use more than two balls in a frame (and the only case where the extra balls after a spare or strike only count once). Write the function bowling(balls) which is passed a list of integers indicated the number of pins knocked down for each ball. You can assume that the list has exactly the right number of entries: it won't stop before the end of the tenth frame, nor go beyond it.


bowling([9, 1, 0, 10, 10, 10, 6, 2, 7, 3, 8, 2, 10, 9, 0, 9, 1, 10]) → 168
bowling([10, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) → 24
bowling([10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10]) → 300

...Save, Compile, Run (ctrl-enter)

def bowling(balls):

Editor font size %:
Shorter output


Forget It! -- delete my code for this problem

Progress graphs:
 Your progress graph for this problem
 Random user progress graph for this problem
 Random Epic Progress Graph

Python Help

Difficulty: 300 Post-solution available

Copyright Nick Parlante 2017 - privacy