Displaying Deck of Cards

Reply

Join Date: Sep 2009
Posts: 2
Reputation: etypaldo is an unknown quantity at this point 
Solved Threads: 0
etypaldo etypaldo is offline Offline
Newbie Poster

Displaying Deck of Cards

 
0
  #1
Sep 20th, 2009
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

  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
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 151
Reputation: snippsat is an unknown quantity at this point 
Solved Threads: 46
snippsat snippsat is offline Offline
Junior Poster

Re: Displaying Deck of Cards

 
0
  #2
Sep 20th, 2009
One way is like this
Take numbers and add like this "A" count as 1.

  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. >>>
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,013
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 929
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Displaying Deck of Cards

 
0
  #3
Sep 20th, 2009
I am not sure which version of Python you are using, but if you explore this code ...
  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!
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 151
Reputation: snippsat is an unknown quantity at this point 
Solved Threads: 46
snippsat snippsat is offline Offline
Junior Poster

Re: Displaying Deck of Cards

 
0
  #4
Sep 21st, 2009
Just to explain one thing look at vegaseat code.
Does it look like this line?
  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.
  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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,276
Reputation: sneekula has a spectacular aura about sneekula has a spectacular aura about 
Solved Threads: 175
sneekula's Avatar
sneekula sneekula is offline Offline
Nearly a Posting Maven

Re: Displaying Deck of Cards

 
0
  #5
Sep 21st, 2009
As a beginner use for loops first, later on 'list comprehensions' become more natural.
No one died when Clinton lied.
Reply With Quote Quick reply to this message  
Reply

Message:



Similar Threads
Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC