Skip to main content
MT_Head's deck.pop(index) suggestion
Source Link
AJNeufeld
  • 34k
  • 5
  • 39
  • 101

Dealing from the Middle of the Deck

    dealtCard1Number = random.randint(0, len(deck)-1)
    playerHand.append(deck[dealtCard1Number])
    del deck[dealtCard1Number]

Variations of this code is repeated many times.

First, you could simplify this code slightly by using random.randrange(len(deck)).

Second, it could be made into a function:

def deal():
    card_number = random.randrange(len(deck))
    card = deck[card_number]
    del deck[card_number]
    return card

And then using statements like:

    playerHand.append(deal())

But selecting and dealing a random card from the middle of the deck just seems wrong. You want to shuffle the deck once, and then deal cards from the top of the deck:

    deck = [...]
    random.shuffle(deck)

Then:

def deal():
    card = deck[0]
    del deck[0]
    return card

Or, as pointed out by MT_Head in the comments:

def deal():
    return deck.pop(0)

PEP-8

Follow the Python Style Guidelines in PEP-8. For instance, variables should be snake_case, not mixedCase.

Dealing from the Middle of the Deck

    dealtCard1Number = random.randint(0, len(deck)-1)
    playerHand.append(deck[dealtCard1Number])
    del deck[dealtCard1Number]

Variations of this code is repeated many times.

First, you could simplify this code slightly by using random.randrange(len(deck)).

Second, it could be made into a function:

def deal():
    card_number = random.randrange(len(deck))
    card = deck[card_number]
    del deck[card_number]
    return card

And then using statements like:

    playerHand.append(deal())

But selecting and dealing a random card from the middle of the deck just seems wrong. You want to shuffle the deck once, and then deal cards from the top of the deck:

    deck = [...]
    random.shuffle(deck)

Then:

def deal():
    card = deck[0]
    del deck[0]
    return card

PEP-8

Follow the Python Style Guidelines in PEP-8. For instance, variables should be snake_case, not mixedCase.

Dealing from the Middle of the Deck

    dealtCard1Number = random.randint(0, len(deck)-1)
    playerHand.append(deck[dealtCard1Number])
    del deck[dealtCard1Number]

Variations of this code is repeated many times.

First, you could simplify this code slightly by using random.randrange(len(deck)).

Second, it could be made into a function:

def deal():
    card_number = random.randrange(len(deck))
    card = deck[card_number]
    del deck[card_number]
    return card

And then using statements like:

    playerHand.append(deal())

But selecting and dealing a random card from the middle of the deck just seems wrong. You want to shuffle the deck once, and then deal cards from the top of the deck:

    deck = [...]
    random.shuffle(deck)

Then:

def deal():
    card = deck[0]
    del deck[0]
    return card

Or, as pointed out by MT_Head in the comments:

def deal():
    return deck.pop(0)

PEP-8

Follow the Python Style Guidelines in PEP-8. For instance, variables should be snake_case, not mixedCase.

Source Link
AJNeufeld
  • 34k
  • 5
  • 39
  • 101

Dealing from the Middle of the Deck

    dealtCard1Number = random.randint(0, len(deck)-1)
    playerHand.append(deck[dealtCard1Number])
    del deck[dealtCard1Number]

Variations of this code is repeated many times.

First, you could simplify this code slightly by using random.randrange(len(deck)).

Second, it could be made into a function:

def deal():
    card_number = random.randrange(len(deck))
    card = deck[card_number]
    del deck[card_number]
    return card

And then using statements like:

    playerHand.append(deal())

But selecting and dealing a random card from the middle of the deck just seems wrong. You want to shuffle the deck once, and then deal cards from the top of the deck:

    deck = [...]
    random.shuffle(deck)

Then:

def deal():
    card = deck[0]
    del deck[0]
    return card

PEP-8

Follow the Python Style Guidelines in PEP-8. For instance, variables should be snake_case, not mixedCase.