2017-02-15 59 views
0
import random 


Cards = { 
    "Spade": ["2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace"], 
    "Diamond": ["2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace"], 
    "Club": ["2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace"], 
    "Heart": ["2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace"] 

} 

for _ in range(55): 
    r = random.sample(list(Cards), 1) 
    print(r[0]) 

我已經嘗試了更多,並不知道它。我在訪問密鑰的值時遇到問題python隨機字典密鑰,並訪問它

回答

2

像這樣的東西?

>>> random.choice(list(Cards)) 
'Heart' 

然後隨機卡:

>>> suit = random.choice(list(Cards)) 
>>> card = random.choice(Cards[suit]) 
>>> card, suit 
('King', 'Heart') 
+0

呀,然後訪問值,R = random.choice(Cards.keys()) 打印(R [1]) –