How to get this deal card fuction working for a BlackJack game?

Question:

import random

cards = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]

human_cards = []
ia_cards = []

def deal_card(x):
    int(random.choice(cards))
    return x

deal_card(human_cards)
print(human_cards)

When I print it out it gives me the below output:

[]

I want to have this function working so it gives me 2 random cards for both users.

Asked By: Hercules

||

Answers:

Well, someone helped me out with this, so I post the answer in case someone needs it also.

import random
cards = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]

human_cards = []
ia_cards = []

def deal_card(x):
    return random.choice(cards)

human_cards = deal_card(cards)
print(human_cards)
Answered By: Hercules

Keep in mind that if you’re simulating dealing from a deck of cards, your deal is never just a random pick from 52 cards over and over again. It’s a random deal from whatever’s left in a deck of cards.

Let’s create a Deck class with a shuffled list of 52 cards, and a deal_card function which deals a card and then removes it from the deck.

import random

class Deck(object):
  def __init__(self):
    suits = ('Hearts', 'Diamonds', 'Spades', 'Clubs')
    faces = ('2', '3', '4', '5', '6', '7', '8', '9', '10', 'Jack', 'Queen', 'King', 'Ace')
    self.deck = [(suit, face) for suit in suits for face in faces]
    random.shuffle(self.deck)
  def deal_card(self):
    try:
      return self.deck.pop()
    except IndexError: 
      return None
Answered By: Chris
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.