943,910 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 793
  • Python RSS
Sep 20th, 2009
0

Displaying Deck of Cards

Expand Post »
I am currently learning Python and how to work with start and stop indexing. I am doing a project from the book I am learning from and I'm stumped...I need to display a full deck of cards, with

Python Syntax (Toggle Plain Text)
  1. numbers = ("A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K")
  2. suits = ("c", "h", "s", "d")

so that it displays the entire deck, paired and ordered to look like:

Ac Ah As Ad
2c 2h 2s 2d
3c 3h 3s 3d
.
.
.
.
Qc Qh Qs Qd
Kc Kh Ks Kd

so far all I have are the numbers and suits set up but I keep trying to do things way too complicated with for loops, so i completely deleted my code and came here for fresh ideas.

Thanks
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
etypaldo is offline Offline
6 posts
since Sep 2009
Sep 20th, 2009
0

Re: Displaying Deck of Cards

One way is like this
Take numbers and add like this "A" count as 1.

python Syntax (Toggle Plain Text)
  1. >>> numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] + 'Jack Queen King'.split()
  2. >>> numbers
  3. [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 'Jack', 'Queen', 'King']
  4. >>>
  5. >>> suits = ['diamonds', 'clubs', 'hearts', 'spades']
  6. >>> suits
  7. ['diamonds', 'clubs', 'hearts', 'spades']
  8.  
  9. >>> deck = ['%s of %s' % (n, s) for n in numbers for s in suits]
  10. >>> deck
  11. # the hole deck
  12.  
  13. And we can make it look better.
  14. >>> from pprint import pprint
  15. >>> pprint(deck[:10]) #52 gives the hole deck
  16. ['1 of diamonds',
  17. '1 of clubs',
  18. '1 of hearts',
  19. '1 of spades',
  20. '2 of diamonds',
  21. '2 of clubs',
  22. '2 of hearts',
  23. '2 of spades',
  24. '3 of diamonds',
  25. '3 of clubs']
  26. >>>
Reputation Points: 280
Solved Threads: 278
Master Poster
snippsat is online now Online
771 posts
since Aug 2008
Sep 20th, 2009
0

Re: Displaying Deck of Cards

I am not sure which version of Python you are using, but if you explore this code ...
python Syntax (Toggle Plain Text)
  1. rank = ("A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K")
  2. suit = ("c", "h", "s", "d")
  3.  
  4. deck = []
  5. for r in rank:
  6. for s in suit:
  7. card = r + s
  8. deck.append(card)
  9.  
  10. print( deck )
... you can see the the list called deck is almost ordered the way you want it. All you need to do now is to print four of the list's elements in a row and then slip in a newline character. Put your thinking hat on!
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Sep 21st, 2009
0

Re: Displaying Deck of Cards

Just to explain one thing look at vegaseat code.
Does it look like this line?
Python Syntax (Toggle Plain Text)
  1. deck = ['%s of %s' % (n, s) for n in numbers for s in suits]
This is called list List Comprehensions
I can wite it like this.
Python Syntax (Toggle Plain Text)
  1. deck = []
  2. for n in numbers:
  3. for s in suits:
  4. card = '%s of %s' % (s, n)
  5. deck.append(card)
Then you see it not so differnt.
Reputation Points: 280
Solved Threads: 278
Master Poster
snippsat is online now Online
771 posts
since Aug 2008
Sep 21st, 2009
0

Re: Displaying Deck of Cards

As a beginner use for loops first, later on 'list comprehensions' become more natural.
Reputation Points: 961
Solved Threads: 211
Nearly a Posting Maven
sneekula is offline Offline
2,413 posts
since Oct 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: building pymedia
Next Thread in Python Forum Timeline: Problem with Python Loop





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC