0

M code works however I want it to use Ace Jack Queen and King instead of 1, 11,12,13 however I dont know how to change my code so it will do it.

# imports random
import itertools, random

# make a deck of cards
deck = list(itertools.product(range(1, 14), ['Spade', 'Heart', 
'Diamond', 'Club']))

# shuffles the deck
random.shuffle(deck)

# draw five cards
howmany = int(input('How many cards do you want to display? '))
print('You got:')
for i in range(howmany):
    print(deck[i][0], 'of', deck[i][1])

Current Results.

How many cards do you want to display? 4 You got: 11 of Diamond 7 of Club 6 of Diamond 8 of Diamond

However I want it to output for the first one for example,

Jack of Diamond

2 Answers2

0

This is how I'd do it. Notice that I changed the last bit of your code mostly. This is to provide a use-case of tuple unpacking, get dictionary method and f-strings:

# imports random
import itertools, random

# make a deck of cards
deck = list(itertools.product(range(1, 14), ['Spade', 'Heart', 
'Diamond', 'Club']))

# shuffles the deck
random.shuffle(deck)

# have a dict of named cards
cards = {11: "Jack", 12: "Queen", 13: "King", 1:"Ace"}

# draw five cards
howmany = int(input('How many cards do you want to display? '))
print('You got:')
for i in range(howmany):
    card, suit = deck[i] # unpack the tuple to have cleaner code
    print(f"{cards.get(card, card)} of {suit}") # use f-string together with get
gstukelj
  • 2,291
  • 1
  • 7
  • 20
0
import itertools, random

deck = list(itertools.product(['Ace']+ list(range(2,11)) + ['jack','Queen','King'] ,['Spade','Heart','Diamond','Club']))

random.shuffle(deck)

print("You got:")
for i in range(1):
   print(deck[i][0], "of", deck[i][1])
Captain Jack Sparrow
  • 971
  • 1
  • 11
  • 28