<p>我试着为游戏中的项目建立一个列表,我不得不在我的程序中多次调用它。我注意到输入并没有存储在我的列表中,它每次都会替换它。你知道吗</p>
<p>我使用了<code>playeritems.append()</code>和<code>playeritems.extend()</code>,但不起作用。你知道吗</p>
<pre><code>def addbackpack():
global playeritems
gameitems= ["sword", "potion"]
playeritems = []
print ("\nWhat would you like to add to your backpack? The sword or potion?\n")
p1_additem = str(input())
if p1_additem in gameitems:
playeritems.append(p1_additem)
print ("\nYou added",p1_additem,"to your backpack.\n")
else:
print ("\nThat is not a choice!\n")
return addbackpack()
addbackpack()
print (playeritems)
addbackpack()
print (playeritems)
</code></pre>
<p>这是我第一次输入剑,第二次输入药剂后的准确结果:</p>
<pre><code>What would you like to add to your backpack? The sword or potion?
sword
You added sword to your backpack
['sword']
What would you like to add to your backpack? The sword or potion?
potion
You added potion to your backpack
['potion']
</code></pre>