I'm doing a bit of code review to help you get started with python, I stripped out your comments (don't say what, say why, by the way), and added my own.
import random # don't forget the module for shuffle
class Card(object):
suit_names = ["Clubs", "Diamonds", "Hearts", "Spades"]
rank_names = [None, "Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"] # Start with your first one, you could then iterate over this list instead of from 1 up to but not including 14.
def __init__(self, suit, rank):
self.suit = suit
self.rank = rank
def getRank(self): # why not use Python's builtin property for getters?
return self.rank
def getSuit(self):
return self.suit
def __str__(self):
return '%s of %s' % (Card.rank_names[self.rank], Card.suit_names[self.suit])
class Deck(object):
def __init__(self):
self.cards = []
for suit in range(4): # 0-3, again, Pythonic to iterate over the list,
for rank in range(1, 14): # 1-13, why start with 1 here, and 0 with suit?
card=Card(suit, rank)
self.cards.append(card)
def shuffle(self):
random.shuffle(self.cards) # call shuffle from the random module,
def main():
selfHand=[]
s,r=input("Input your first downcard's suit and rank, separated by a comma" )
card=Card(s,r)# here you're creating a new card, why not find it in the deck?
selfHand.append(card)
deck.cards.remove(card) # and this deletes the old card, you'll need to be careful to put it back if you do this again.
So I might do this instead:
import random # don't forget the module for shuffle
class Card(object):
suit_names = ["Clubs", "Diamonds", "Hearts", "Spades"]
rank_names = ["Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"]
def __init__(self, suit, rank):
self._suit = Card.suit_names.index(suit)
self._rank = Card.rank_names.index(rank)
@property # Python's built-in property, treat rank as an attribute!
def rank(self):
return Card.rank_names[self._rank]
@property
def suit(self):
return Card.suit_names[self._suit]
def __repr__(self): # this will reproduce a card, and will echo on your REPL
return 'Card(%s, %s)' % (self.rank, self.suit) # see how we get attrs now?
def __str__(self):
return '%s of %s' % (self.rank, self.suit)
class Deck(object):
def __init__(self):
self.cards = []
for suit in Card.suit_names:
for rank in Card.rank_names:
card=Card(suit, rank)
self.cards.append(card)
def shuffle(self):
random.shuffle(self.cards) # avoid dumping in your namespace where you might overwrite, call shuffle from the random module
def main():
deck = Deck()
selfHand=[]
response=raw_input("Input suit and rank, as in 'Ace of Clubs' or '10 of Hearts'" )
# and now don't create and delete more cards so the accounting is easier.
deck_str = [str(c) for c in deck.cards]
selfHand.append(deck.cards.pop(deck_str.index(response)))
print selfHand
print 'response not in deck_str:', response not in deck_str
And when I enter Ace of Clubs
:
[Card(Ace, Clubs)]
response not in deck.cards: True