擅长:python、mysql、java
<p>你在最后一个街区有多处失误。您可以打开<code>scoreList.txt</code>,但在<code>with .. as</code>块之外执行写操作。在此块之外,文件将关闭。另外,由于您使用的是<code>with .. as</code>,因此最终不必手动关闭文件</p>
<p>然后,您迭代所有行,并为不包含它的每一行编写名称,可能会重复很多次。然后用<code>'w'</code>打开文件,告诉它覆盖。如果要追加,请使用<code>'a'</code></p>
<p>试着这样做:</p>
<pre><code>with open('scoreList.txt', 'r') as scoreRead:
data = scoreRead.readlines()
with open('scoreList.txt', 'a') as scoreWrite:
if userName + "\n" not in data:
scoreWrite.write(userName + "\n")
</code></pre>
<p>另外请注意,您当前正在将每个名字写入得分列表,而不仅仅是那些赢得比赛的名字</p>