擅长:python、mysql、java
<p><code>infile.read()</code>将获取整个文件,而不是单个部分。如果您想要单独的部分,就必须将它们分开(按空格)并去掉空白(即<code>\n</code>)。</p>
<p>强制性一行:</p>
<pre><code>contents = infile.read().strip().split()
</code></pre>
<p>然后您希望遍历<code>contents</code>的内容,因为这是唯一值得遍历的内容。<code>infile</code>已经用尽,对<code>read()</code>的后续调用将生成空字符串。</p>
<pre><code>for num in contents:
amount = float(num)
# more code here
average = total / len(contents) # you can use the builtin len() method to get the length of contents instead of counting yourself
</code></pre>