擅长:python、mysql、java
<p>您的代码看起来很好,只是最后并没有实际调用<code>c1.close</code>。你知道吗</p>
<p>您需要在其后面添加<code>()</code>才能执行此操作:</p>
<pre><code>import random
names = "Balo", "Bandugl", "Baroro", "Cag", "Charoth", "Duglinglabat", "Dulko", "Fangot"
rand_name = random.choice(names)
c1 = open("character_one.txt", "w")
c1.write(rand_name)
c1.close()
</code></pre>
<hr/>
<p>这就是为什么使用<a href="http://effbot.org/zone/python-with-statement.htm" rel="nofollow">with-statement</a>打开文件是个好主意:</p>
<pre><code>import random
names = "Balo", "Bandugl", "Baroro", "Cag", "Charoth", "Duglinglabat", "Dulko", "Fangot"
rand_name = random.choice(names)
with open("character_one.txt", "w") as c1:
c1.write(rand_name)
</code></pre>
<p>这样做可以确保文件在完成时关闭。你知道吗</p>