jeudi 26 septembre 2019

How do I get random values from nested dictionaries?

So, I'm fairly new to Python and trying to teach myself with the help of some books right now. I'm currently working on a personal project where I simulate a hand of playing cards. I have a dictionary with four nested dictionaries representing each suit, and for each of those dictionaries I have all cards that would be present.

I've tried searching for ways to get these values, but everything I've found has either been for Python 2.x and doesn't work anymore, or only seems to go one dictionary deep.

deck = {
    'Spades': {'Aces': 1, 'Twos': 1, 'Threes': 1, 'Fours': 1, 'Fives': 1, 'Sixes': 1, 'Sevens': 1, 'Eights': 1, 'Nines': 1, 'Tens': 1, 'Jacks': 1, 'Queens': 1, 'Kings': 1},
    'Diamonds': {'Aces': 1, 'Twos': 1, 'Threes': 1, 'Fours': 1, 'Fives': 1, 'Sixes': 1, 'Sevens': 1, 'Eights': 1, 'Nines': 1, 'Tens': 1, 'Jacks': 1, 'Queens': 1, 'Kings': 1},
    'Hearts': {'Aces': 1, 'Twos': 1, 'Threes': 1, 'Fours': 1, 'Fives': 1, 'Sixes': 1, 'Sevens': 1, 'Eights': 1, 'Nines': 1, 'Tens': 1, 'Jacks': 1, 'Queens': 1, 'Kings': 1},
    'Clubs': {'Aces': 1, 'Twos': 1, 'Threes': 1, 'Fours': 1, 'Fives': 1, 'Sixes': 1, 'Sevens': 1, 'Eights': 1, 'Nines': 1, 'Tens': 1, 'Jacks': 1, 'Queens': 1, 'Kings': 1}
}

I want to create a function that will return a random card, and I can get one random value from the deck variable, using random.choice(list(deck.keys())), but I can't get into those dictionaries, and I feel that it's really simple and I'm just being dumb.




Aucun commentaire:

Enregistrer un commentaire