Member 13982327 Ответов: 0

Как я могу выполнить цикл над каждым fram в массиве frames, чтобы вычислить сумму всех возвращаемых значений вызовов метода frame_score.


Мне нужно написать приложение, которое вычисляет баллы в боулинге. Это больше для прояснения и понимания моих ошибок, потому что я действительно хочу понять, как выполнить эту задачу.

Дополнительно;

Import the frame_score method from 
the file framescore.py

Import the read_ragged_array method from 
   the framescore.py file.
   (See the RaggedArray Example)
   
read inputfile from keyboard, promping user. 
Call the read_ragged_array method to create
   a ragged array of the data in the input_file like
   game1.txt, game2.txt, or game3.txt.
Assign the ragged array to the variable frames.

Initialize score to 0.
For i in the range from 1 to 10 (use range(1, 11))
   if frames[i][0] equals 10 and frames[i+1][0] equals 10
      assign 10 to bonus1.
      assign frames[i+2][0] to bonus2. 
   elsif frames[i][0] equals 10 and frames[i+1][0] < 10
      assign frames[i+1][0] to bonus1.
      assign frames[i+1][1] to bonus2.
   elsif frames[i][0] + frames[i][1] equals 10
      assign frames[i+1][0] to bonus1.
      assign zero to bonus2.
   else
      assign zero to bonus1.
      assign zero to bonus2.
   end if
   # Next line is actual Python statement.   
   score += framescore(frames[i], bonus1, bonus2)
End loop.

What I have tried:

bowling.py
<pre># import the frame_score method from the file framescore.py
from framescore import frame_score
# import the read_ragged_array method from the framescore.py file.
from framescore import read_ragged_array

# read inputfile from keyboard, promping user.
# call the read_ragged_array method to create
# a ragged array of the data in the input_file like
# game1.txt, game2.txt, or game3.txt.
input_file = input("Enter name of input file ('game1.txt, game2.txt, game3.txt'):")

# assign the ragged array to the variable frames.
frames = read_ragged_array(input_file)

# Initialize score to 0.
score = [0]

for i in range(1, 11):
    if frames[i][0] == 10 and frames[i+1][10] == 10:
        bonus1 = 10
        bonus2 = frames[i+2][0]
    elif frames[i][0] == 10 and frames[i+1][0] < 10:
        bonus1 = frames[i+1][0]
        bonus2 = frames[i+1][1]
    elif frames[i][0] + frames[i][1] == 10:
        bonus1 = frames[i+1][1]
        bonus2 = 0
    else:
        bonus1 = 0
        bonus2 = 0
# next line is actual Python statement.
    while True:
        score += frame_score(frames[i], bonus1, bonus2)
#End loop.

print(score)


framescore.py
def frame_score(the_frame, bonus1, bonus2):
    # Case where frame is a strike
    if the_frame[0] == 10: 
        return 10 + bonus1 + bonus2
    # Case where frame is a spare
    elif the_frame[0] + the_frame[1] == 10: 
        return 10 + bonus1
    # Case where frame is an open frame
    else:
        return the_frame[0] + the_frame[1] 

def read_ragged_array(input_file):
    frames = [["Frame0"]]
    f = open(input_file, "r")
    line = f.readline( )
    while line:
        fields = line.split(" ")
        row = [ ]
        for field in fields:
            row.append(int(field.strip( )))
        frames.append(row)
        line = f.readline( )
    return frames
    
frames = read_ragged_array("game1.txt")
print(frames)

frames = read_ragged_array("game2.txt")
print(frames)

frames = read_ragged_array("game3.txt")
print(frames)

# Store the frames in an array named frames.
# Also, instead of initializing the frames array to [ ],
# initialize it to [["Frame0"]] so that the first actual bowling frame
# in frames is at index 1. ["Frame0"] is a placeholder at index 0.
# Loop over each frame in the frames array to compute the sum of all the return values
# of the frame_score method calls.

Richard MacCutchan

В чем же вопрос?

0 Ответов