H. Soto
H. Soto

Reputation: 49

Printing a deck of cards (Python)

Trying to create a deck of cards and printing them randomly. It gives me this message when I try to run my code:

Traceback (most recent call last):
  File "C:\Users\Tobi01\Downloads\blackjack (1).py", line 31, in <module>
    deal_deck()
  File "C:\Users\Tobi01\Downloads\blackjack (1).py", line 14, in deal_deck
    card = random.choice(DECK)
  File "C:\Python31\lib\random.py", line 256, in choice
    return seq[int(self.random() * len(seq))]  # raises IndexError if seq is empty
IndexError: list index out of range

Can someone tell me what I'm doing wrong?

 import random


def create_deck():
    for suit in SUITS:
        for pip in PIPS:
            card = (suit,pip)
            deck.apppend(card)




def deal_deck():
    card = random.choice(DECK)
    deck.remove(card)
    return card



CLUB = "\u2663"
HEART = "\u2665"
DIAMOND = "\u2666"
SPADE = "\u2660"

PIPS = ("A","2","3","4","5","6","7","8","9","10","J","Q","K")
SUITS = (CLUB, SPADE, DIAMOND, HEART)

DECK = []

create_deck
deal_deck()




for i in range(13):
    for j in range(4):
        pip,suit = deal_deck
        print(suit + pip, end = " ")
    print()

Upvotes: 1

Views: 3895

Answers (2)

Jean-Fran&#231;ois Fabre
Jean-Fran&#231;ois Fabre

Reputation: 140168

Several errors here

  • create_deck without parentheses does nothing, so you're not initializing your deck
  • You initialize an upper case DECK (not the same, as python is case sensitive), so when you perform the deck shuffle you get an error because DECK is an empty list.

(and MooingRawr pointed out the next errors you'll get if you fix those ones :))

Upvotes: 2

MooingRawr
MooingRawr

Reputation: 4991

import random


def create_deck():
    for suit in SUITS:
        for pip in PIPS:
            card = (suit,pip)
            deck.append(card) #append not apppend




def deal_deck():
    card = random.choice(deck) #deck not DECK
    deck.remove(card)
    return card



CLUB = "\u2663"
HEART = "\u2665"
DIAMOND = "\u2666"
SPADE = "\u2660"

PIPS = ("A","2","3","4","5","6","7","8","9","10","J","Q","K")
SUITS = (CLUB, SPADE, DIAMOND, HEART)

deck = [] #deck not DECK 

create_deck() #create_deck() not create_deck
#deal_deck() #why remove a card for no reason no...


for i in range(13):
    for j in range(4):
        pip,suit = deal_deck() #deal_deck() not deal_deck
        print(suit + pip, end = " ")
    print()

So many errors, give it a read,

You were mixing DECK and deck they are not interchangeable.

You were trying to call a function without calling a function. Functions needs to be called with a () at the end of it.

You miss spelled append

And you removed a card before going into the for loop calling deal_deck() outside of the for loop, and that's why you didn't have 52 cards going into the for loop.

There's ways to improve this code, but I just wanted you to understand your mistakes so you wouldn't make them in the future.

Upvotes: 3

Related Questions