G
The correct input data is given in the format shown in this line.score_lines = [2, 5, 0, 1, 8, 2, 6, 2, 10, 2, 7, 0, 10, 10, 10, 9, 1, 3]
In this case, your code is broken down by a variety of points other than the point that you find yourself, "Del's indexes will be broken."i % 2 == 0In terms of saying "iis used to determine "that is pointing to the first throw, but as far as the data is seen, the first throw is10The second data is not present. Soi % 2 == 0It is not possible to determine whether or not the first pitch by the condition.After all the spares have been processed in your code, you can process the strike. Therefore, even if there is a spare after the strike, the data isscore_linesThe Strike score cannot be processed correctly because it is removed from it.for i in range(len(score_lines))and the range of repetition is determined,iHomescore_linesmay point to the last element of thescore_lines[i+1]Homescore_lines[i+2]Access So, at alldelindex out of rangeTherefore, you can do the following corrections.iInstead of determining whether or not from the command line,ibut always point to the firstiControl the touch panel. 上記 to the aboveiIf you think that the data before the data pointed to is processed, it will not be processed to say that the data is deleted by del.Always check the range when specifying the list index.What is the code like?score_lines = [2, 5, 0, 1, 8, 2, 6, 2, 10, 2, 7, 0, 10, 10, 10, 9, 1, 3]
ans_sum =[]
pin_num = 10
i = 0
while len(ans_sum) < 10:
first_throw_pins = score_lines[i]
second_throw_pins = score_lines[i+1] if i+1 < len(score_lines) else 0
third_throw_pins = score_lines[i+2] if i+2 < len(score_lines) else 0
if first_throw_pins == pin_num:
# strike
ans_sum.append(first_throw_pins + second_throw_pins + third_throw_pins)
i += 1
elif first_throw_pins + second_throw_pins == pin_num:
# spare
ans_sum.append(first_throw_pins + second_throw_pins + third_throw_pins)
i += 2
else:
ans_sum.append(first_throw_pins + second_throw_pins)
i += 2
print(ans_sum)
whileIn terms ofi score_linesdoes not work unless the data is unmatched, but the score calculation from the correct 10 frames (actually lists the scores of each frame)ans_sumIt should be possible to put in. Contact UsGenerally speaking, if you proceed with the processing as "remove processed data" as the original code, the processing speed will be slower than the index will be shifted. As far as possible, it is better to think that "remove processed data" should be avoided.