<p>你的<code>playerList</code>对象仍然是字符串列表。你的循环:</p>
<pre><code>for player in playerList:
player = Player(self.startingStack)
currentPlayer += 1
if currentPlayer == self.numPlayers:
break
</code></pre>
<p>将<code>player</code>重新绑定到<code>Player()</code>实例,但这不会更改<code>playerList</code>对象</em>。事实上,当您进入下一个迭代时,<code>Player()</code>实例再次被丢弃,因为没有其他引用它。你知道吗</p>
<p><code>for player in playerList</code>将<code>player</code>依次绑定到列表中的每个字符串,但引用不起相反的作用,<code>player</code>只是另一个Python名称,另一个引用包含在</em><code>playerList</code>中的同一对象。将不同的对象赋给它也不会改变从中获取字符串的列表。你知道吗</p>
<p>你可能想在这里建立一个新的列表:</p>
<pre><code>def startGame(self):
newDeck = Deck()
playerList = ['p1', 'p2', 'p3', 'p4', 'p5', 'p6', 'p7', 'p8']
players = []
for player in playerList[:self.numPlayers]:
player = Player(self.startingStack)
players.append(player)
totalPlayers = self.numPlayers
# GAME STARTS -
newCard = []
for player in players:
newCard.append(player.generateCard(newDeck))
</code></pre>
<p>但是,您在这里完全忽略了<code>playerList</code>中的字符串;也可以只构建列表而不咨询以下内容:</p>
<pre><code>def startGame(self):
newDeck = Deck()
players = [Player(self.startingStack) for _ in range(self.numPlayers)]
newCard = [player.generateCard(newDeck) for player in players]
</code></pre>
<p>但是你也不需要<code>players</code>作为一个单独的列表:</p>
<pre><code>def startGame(self):
newDeck = Deck()
newCard = [Player(self.startingStack).generateCard(newDeck)
for _ in range(self.numPlayers)]
</code></pre>