【发布时间】:2015-01-18 05:43:29
【问题描述】:
这里是上下文的代码。
def processScores( file, score):
#opens file using with method, reads each line with a for loop. If content in line
#agrees with parameters in elif statements, executes code in if statment. Otherwise, ignores line
with open(file,'r') as f:
for line in f: #starts for loop for all if statements
line = line.strip()
if line.isdigit():
start = int(line)
score.initialScore(start)
print(line)#DEBUG TEST**** #checks if first line is a number if it is adds it to intial score
elif len(line) == 0:
print(line)#DEBUG TEST****
continue #if a line has nothing in it. skip it
elif line == 'o' or line == 'O':
amount = next(f)
print(line)#DEBUG TEST****
score.updateOne(amount) #if line contains single score marker, Takes content in next line and
#inserts it into updateOne
elif line == 'm'or line == 'M':
scoreList = next(f)
lst = []
for item in scoreList:
print(line)#DEBUG TEST****
lst.append(item)
score.updateMany(lst) # if line contains list score marker, creates scoreList variable and places the next line into that variable
# creates lst variable and sets it to an empty list
# goes through the next line with the for loop and appends each item in the next line to the empty list
# then inserts newly populated lst into updateMany
elif line == 'X':
print(line)#DEBUG TEST****
score.get(self)
score.average(self) # if line contains terminator marker. prints total score and the average of the scores.
# because the file was opened with the 'with' method. the file closes after
我正在尝试的想法是使用如下所示的文件:
50
哦
30
哦
40
M
10 20 30
o
5
米
1 2 3
X
如果代码看到“O”或“o”,那么它需要在代码中取下一行并将其添加到运行分数中。但是下一行是空格...所以我需要跳到“O”或“o”之后的第二行。
我正在考虑为此做一个例外,但在我走这条路之前,我想看看是否有人可能知道更好的方法。
【问题讨论】: