By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,767 Members | 1,995 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,767 IT Pros & Developers. It's quick & easy.

just need help with a card game

P: 8
I got my code to work be the print out go's down on a angle is there anyway to stop this from happing. P.S sorry for taking up your time


Expand|Select|Wrap|Line Numbers
  1. class Card:
  2.  
  3.     suitList = ["Hearts", "Diamonds", "Clubs", "Spades"]
  4.     rankList = ["Invalid", "Ace", "2", "3", "4", "5", "6", "7",
  5.                 "8", "9", "10", "Jack", "Queen", "King"]
  6.  
  7.     def __init__ (self, suit=0, rank=1):
  8.         if suit >= 0 and suit <= 3:
  9.             self.suit = suit
  10.         else:
  11.             self.suit = 0
  12.         if rank >= 1 and rank <= 13:
  13.             self.rank = rank
  14.         else:
  15.             self.rank = 1
  16.  
  17.     def __str__(self):
  18.         return self.rankList[self.rank] + " of " + self.suitList[self.suit]
  19.  
  20.     def __cmp__(self, other):
  21.         if self.rank > other.rank:
  22.             return 1
  23.         if self.rank < other.rank:
  24.             return -1
  25.         if self.suit > other.suit:
  26.             return 1
  27.         if self.suit < other.suit:
  28.             return -1
  29.         return 0
  30.  
  31. class Deck:
  32.  
  33.     def __init__(self):
  34.         self.cards = [] 
  35.         for suit in range(4):
  36.             for rank in range(1,14):
  37.                 self.cards.append(Card(suit, rank))
  38.  
  39.     def __str__(self):
  40.         s = ""
  41.         for i in range(len(self.cards)):        
  42.             s = s + (" " * i) + str(self.cards[i]) + "\n"
  43.         return s
  44.  
  45.     def printDeck(self):
  46.         for card in self.cards:
  47.             print card
  48.  
  49.  
  50. def main():
  51.  
  52.     deck1 = Deck()
  53.     print "Original deck:"
  54.     print deck1
  55.  
  56.  
  57. main()
if some one can help me that will be gr8
May 22 '07 #1
Share this Question
Share on Google+
5 Replies


P: 8
o sorry and i need to add spaces between them as well :-(
May 22 '07 #2

bvdet
Expert Mod 2.5K+
P: 2,851
'Deck' method:
Expand|Select|Wrap|Line Numbers
  1.     def __str__(self):
  2.         return '\n'.join([str(self.cards[i]) for i in range(len(self.cards))])
May 22 '07 #3

P: 8
thank you so much i need that help
May 23 '07 #4

bvdet
Expert Mod 2.5K+
P: 2,851
thank you so much i need that help
You are welcome. I am pleased to help.
May 23 '07 #5

bvdet
Expert Mod 2.5K+
P: 2,851
Actually, I like this better:
Expand|Select|Wrap|Line Numbers
  1. ....def __iter__(self):
  2.         for card in self.cards:
  3.             yield card
  4.  
  5.     def __str__(self):
  6.         return '\n'.join([str(c) for c in self])
May 23 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.