0

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.

finefoot
  • 9,914
  • 7
  • 59
  • 102

3 Answers3

2

I can get one random value from the deck variable, using random.choice(list(deck.keys()))

Why don't you just repeat that? First, randomly select the suit, then randomly select the card.

suit = random.choice(list(deck))
value = random.choice(list(deck[suit]))
print(suit, value)

Might print for example:

Diamonds Fives

(You can omit the .keys() when converting the keys to a list by the way.)

Also, I don't know if you need that or what those values are for, but you can then access the values inside for each card with deck[suit][value] like

print(deck[suit][value])

Which prints:

1
finefoot
  • 9,914
  • 7
  • 59
  • 102
  • This solved the problem right quick, thank you. I figured it was probably something simple, but I'm not very familiar yet with the syntax of Python and I just couldn't get my solutions to work. I am implementing this solution, and I much appreciate the help. – robin_of_pride Sep 26 '19 at 22:14
0

Is this what your looking for?

suit = random.choice(list(deck))
value = random.choice(list(deck[suit]))

For example:

from random import choice

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}}

suit = choice(list(deck))
value = choice(list(deck[suit]))

print(suit, value)

Prints:

Clubs Sixes
Jab
  • 26,853
  • 21
  • 75
  • 114
0

First, select a suit randomly as you do now.

suit = random.choice(list(deck.keys()))

Then access the dictionary of cards of that particular suit, and randomly choose the card in the same way.

random.choice(list(deck[suit].keys()))