<blockquote>
<p>This is the same problem simplified</p>
</blockquote>
<pre><code>x = 1
def call(x):
x = x + 1
print x
return x
call(x)
</code></pre>
<p>然后呢?你期望什么?全局<code>x</code>将在最后一行之后自动更新吗?抱歉,事情不是这样的。在<code>call()</code>中,<code>x</code>是一个局部名称,与外部全局<code>x</code>完全无关。当您调用<code>call(x)</code>时。如果要更新全局<code>x</code>,必须明确地重新绑定它:</p>
<pre><code>def call(x):
x = x + 1
print x
return x
x = 1
x = call(x)
</code></pre>
<p>我强烈建议你读一下:<a href="https://nedbatchelder.com/text/names.html" rel="nofollow noreferrer">https://nedbatchelder.com/text/names.html</a></p>
<p>编辑:</p>
<blockquote>
<p>"I want it so when I run the hit() function a second time, the total is the total of the last time I used it" </p>
</blockquote>
<p>你的责任(我是说调用这个函数的代码的责任)是把总数存储在某个地方,然后在下一次调用时传回:</p>
<pre><code># Q&D py2 / py3 compat:
try:
# py2
input = raw_input
except NameError:
# py3
pass
def call(x):
x = x + 1
print(x)
return x
x = 1
while True:
print("before call, x = {}".format(x))
x = call(x)
print("after call, x = {}".format(x))
if input("play again ? (y/n)").strip().lower() != "y":
break
</code></pre>