0

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)?
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • 2
    `__init__` is spelled with two underscores on each side. – user2357112 Nov 10 '20 at 14:55
  • Does this answer your question? [What is the best way to create a deck of cards?](https://stackoverflow.com/questions/41970795/what-is-the-best-way-to-create-a-deck-of-cards) – Tomerikoo Nov 10 '20 at 15:51

2 Answers2

1
  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.
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
0

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
]
jottbe
  • 4,228
  • 1
  • 15
  • 31