擅长:python、mysql、java
<p>尝试将其附加到<code>list</code>。你知道吗</p>
<p><strong>代码:</strong></p>
<pre><code>import random
cards = {1.1:"Ace of Spades",
1.2:"Ace of Clubs",
1.3:"Ace of Diamonds",
1.4:"Ace of Hearts",
2.1:"Two of Spades",
2.2:"Two of Clubs",
2.3:"Two of Diamonds",
2.4:"Two of Hearts",
3.1:"Three of Spades",
3.2:"Three of Clubs",
3.3:"Three of Diamonds",
3.4:"Three of Hearts",
4.1:"Four of Spades",
4.2:"Four of Clubs",
4.3:"Four of Diamonds",
4.4:"Four of Hearts"}
used_cards = []
def add_card():
used_cards.append(cards[random.randint(1, 5) + random.randint(1, 5) * 0.1])
add_card()
add_card()
print used_cards
</code></pre>
<p><strong>输出:</strong></p>
<pre><code>['Three of Diamonds', 'Three of Spades']
</code></pre>
<p><strong>PS:</strong>
考虑到我的字典不完整,这一部分</p>
<pre><code>cards[random.randint(1, 5) + random.randint(1, 5) * 0.1
</code></pre>
<p>生成随机数以获得随机卡。您可能需要将范围(1,x)修改为所需的数字。你知道吗</p>
<p><strong>编辑:</strong></p>
<p>正如@Peter DeGlopper所说,你可能想改变你在字典中存储卡片的方式。可以使用<code>(1, 1)</code>这样的元组而不是<code>1.1</code>,以避免浮点不准确。你知道吗</p>