Sean GulleyRyan
Sean GulleyRyan

Reputation: 1

How can I make this "Card" class generate a list of Card objects?

I am trying to make a deck of cards for my beginner Python class, and this is the code I have so far:

import random
import itertools

class Card:
    def _init_(self, value, suit):
        self.value = value
        self.suit = suit
        
value = ['Ace', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'Jack','Queen', 'King']
suit = ['Spades', 'Diamonds', 'Hearts', 'Clubs']

What I intend to do (and what I think it's doing), is using the "values" and "suits" to create 52 different possible combinations, like a real deck of cards. Now, I just want to print a list of all of these 52 combinations. So I have three questions:

  1. Is all of this correct so far?
  2. How can I print a list of these cards?
  3. Is it possible to have two different lists that are changeable (for example, be able to draw a new card into a deck/play a card)?

Upvotes: 0

Views: 394

Answers (2)

user14513063
user14513063

Reputation:

  1. Yes, it's correct, you only need two underscores on each side of "init" (__init__).

  2. Make a for loop in another for loop:

cards = []
for v in value:
    for s in suit:
        cards.append(Card(v, s))

  1. You can append(), pop() e.t.c. to change lists.

Upvotes: 1

jottbe
jottbe

Reputation: 4521

Alternatively, if you are a fan of python generators, you can write your card creation like this:

cards= [
    Card(v, s) for v in value for s in suit
]

Upvotes: 0

Related Questions