<p>您可以使用列表理解、max()和自定义排序键来完成。我不确定我是否正确理解了这个问题,但我把它解释为:赢家是价值最高的牌,和王牌一样的牌</p>
<pre><code>def getCardValue(inputVal): #custom key func that calculates zero as a 10
num = int(inputVal[:-1])
if num == 0:
return 10
return num
trumpCard = '2D'
tricks = ('9D', '9S', '3S', '0D')
potentialWinners = [x for x in tricks if x[-1] == trumpCard[-1]] #remove cards that don't have same suit as trumpCard
print(max(potentialWinners, key=getCardValue)) #get max card value while calculating zero as 10
</code></pre>
<h2>修改getCardValue以使用J、Q、K、A</h2>
<pre><code>def getCardValue(inputVal): #custom key func that calculates zero as a 10
inputVal = inputVal[:-1]
specialValues = {"J": 11, "Q": 12, "K": 13, "A": 14}
if inputVal in specialValues.keys():
return specialValues[inputVal]
num = int(inputVal)
if num == 0:
return 10
return num
</code></pre>
<h2>修改以获得获胜者的索引</h2>
<p>将最后两行更改为:</p>
<pre><code>winner = max(potentialWinners, key=getCardValue) #get max card value while calculating zero as 10
print("player", tricks.index(winner), "wins with the card", winner)
</code></pre>