<p>所以你有5个记录,每个记录有2行。第一个任务是正确地接收这些信息。您可以通过<code>fin.readline()</code>或<code>next(fileobject)</code>获得一行代码,这也适用于python3.x。你知道吗</p>
<pre><code>weights = ( 0.3, 0.1, 0.3 )
with open('datafile') as fin: #open file for reading
n = int(next(fin)) #read the first line and figure out how many people there will be
for _ in range(n): #Loop over the records, 2 at a time:
name = next(fin).strip() #read the name, strip off the whitespace.
grades = [float(x) for x in next(fin).split()] #read the grades, make then floats
total = sum( w*g for w,g in zip(weights,grades) )
print name, total
</code></pre>
<p>到目前为止,这与您所拥有的并没有太大区别:</p>
<pre><code>def main():
inFile = open("input.txt","r")
numVals = int(inFile.readline())
for i in range(numVals):
name = inFile.readline() #add a .strip() here
#grades = [float(x) for x in inFile.readline().strip()]
#do the rest of the processing for a single person here
#since you have all their info. If you wait, you'll replace
#the info you currently have with the info for the next person
#You'll continue to do that until the last person meaning
#that at the end of the day, you'll only have the info for the
#last person.
</code></pre>