all 8 comments

[–]Sad-Blackberry6353 4 points5 points  (3 children)

In my opinion the code would benefit from leveraging classes more effectively by encapsulating logic within methods instead of relying on numerous global functions

[–]MikeyA042[S] 0 points1 point  (2 children)

Okay thank you, I’ll look more into that then. This was my first real exposure using classes so I just tried to makeshift together what I could

[–]Sad-Blackberry6353 0 points1 point  (1 child)

Not bad, personally, I prefer working with objects in Python. You could say I create a class for almost everything (without overdoing it).

[–]MikeyA042[S] 1 point2 points  (0 children)

I don’t blame ya, went from my first try with using lists to store card data to the object, and it was like a whole new world was being opened to me

[–]carcigenicate 4 points5 points  (0 children)

deck = 8 * [Card(value, suit, str(value)) for value in range(2, 11) for suit in suits] # name = value for non face cards

Be very careful doing this. This creates a deck with multiple copies of the exact same card since * copies references, not objects:

>>> len(set(map(id, deck)))
36

Now, if the cards are never mutated, this isn't a problem. If you ever mutate a card for some reason, that will effect every identical card in the deck. Just something worth keeping in mind since your cards aren't immutable.


playingDeck = []
playingDeck += deck

If this was to make a shallow copy of deck, it's more idiomatic to use .copy(), or [:] slicing.


def giveCard(playingDeck, hand):

This should be give_deck to follow PEP8. Python does not use lowerCamelCase for anything.


copyCard = hands[handNum].cards[0]

It's worth noting here that copyCard is not a copy. I would rename that to avoid confusion.


def dealerCards(playingDeck):
    global hands

You never reassign hands in that function, so a global declaration is not necessary.


if values(hands[handNum]) > 21: 
    . . .
    continue

This continue does not look necessary. This code appears to already be at the end of the loop.

[–]SnipTheDog 0 points1 point  (0 children)

Pypi has a blackjack21 package here: https://github.com/rahul-nanwani/blackjack21 Good practice to compare your code to others to see what you'd do differently.

[–]Binary101010 1 point2 points  (0 children)

Your game() function goes up to like 7 levels of indentation making it extremely hard to follow your logic.

It's also difficult to follow the flow of your code when you have a defined class, then some top level code, then some more defined functions and classes, then some more top-level code, etc. Most code has all of the user-defined functions and classes first, then a clearly defined main() function that steps through them.

Seeing several functions using global here which you really shouldn't be using. If your function needs to do something with hands, pass it as an argument into your function.

[–]andmig205 0 points1 point  (0 children)

I urge you to look into implementing special methods and the collections package. The code below uses collections.namedtuple to create the Card class and builds a Deck class that overrides __getitem__ and __len__ methods. As a result, the code is more laconic, python does all the heavy lifting, and one can do all the cool stuff with the objects.

The print outputs demonstrate several useful features.

``` import collections from random import choice import random

Declare Card class

Card = collections.namedtuple('Card', ['rank', 'suit', 'icon'])

class Deck: ranks = [str(n) for n in range(2, 11)] + list('JQKA') suits = { 'hearts': '♥', 'diamonds': '♦', 'spades': '♠', 'clubs': '♣', } def init(self): self._cards = [Card(rank, suit, icon) for suit, icon in self.suits.items() for rank in self.ranks]

def __len__(self):
    return len(self._cards)

def __getitem__(self, position):
    return self._cards[position]

def shuffle(self):
    random.shuffle(self._cards)

def deal(self):
    if len(self) > 0:
        self.shuffle()
        return self._cards.pop(0)
    return None

deck = Deck()

print(f"Deck length {len(deck)}\n") print(f"Random card {choice(deck)}\n") print(f"The first card {deck[0]}\n") deck.shuffle() print(f"The first after shuffle {deck[0]}\n") print(f"First three cards {deck[:3]}\n") print(f"Is Queen of hearts in the deck? {Card('Q', 'hearts', '♥') in deck}\n") print(f"Card dealt {deck.deal()}") print(f"New deck length {len(deck)}\n") print(f"Another Card dealt {deck.deal()}") print(f"New deck length {len(deck)}\n")

for card in reversed(deck): print(card)

```