擅长:python、mysql、java
<p><code>gold</code>不是一个列表。它是一个<em>整数</em>,因此使用<em>加法</em>:</p>
<pre><code>inventory['gold'] += 50
</code></pre>
<p>这使用了增广赋值,对于整数,它相当于:</p>
^{pr2}$
<p>如果您还需要<code>gold</code>作为列表,并希望以<code>[500, 50]</code>作为值,则必须使用列表替换当前值:</p>
<pre><code>inventory['gold'] = [inventory['gold'], 50]
</code></pre>
<p>如果您需要随时间添加多个值,并且不知道<code>gold</code>是列表还是简单整数,并且<em>无法将原始字典</em>更改为<em>始终</em>使用列表,则可以使用异常处理:</p>
<pre><code>try:
inventory['gold'].append(50)
except AttributeError:
# not a list yet
inventory['gold'] = [inventory['gold'], 50]
</code></pre>
<p>但是,如果您以<code>gold</code>始终是一个列表对象来启动</em>,那么维护您的项目会容易得多。在</p>