Reputation: 1
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:
Upvotes: 0
Views: 394
Reputation:
Yes, it's correct, you only need two underscores on each side of "init" (__init__
).
Make a for
loop in another for
loop:
cards = []
for v in value:
for s in suit:
cards.append(Card(v, s))
append()
, pop()
e.t.c. to change lists.Upvotes: 1
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