| | |
Displaying Deck of Cards
![]() |
•
•
Join Date: Sep 2009
Posts: 2
Reputation:
Solved Threads: 0
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
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
Python Syntax (Toggle Plain Text)
numbers = ("A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K") 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
•
•
Join Date: Aug 2008
Posts: 151
Reputation:
Solved Threads: 46
One way is like this
Take numbers and add like this "A" count as 1.
Take numbers and add like this "A" count as 1.
python Syntax (Toggle Plain Text)
>>> numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] + 'Jack Queen King'.split() >>> numbers [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 'Jack', 'Queen', 'King'] >>> >>> suits = ['diamonds', 'clubs', 'hearts', 'spades'] >>> suits ['diamonds', 'clubs', 'hearts', 'spades'] >>> deck = ['%s of %s' % (n, s) for n in numbers for s in suits] >>> deck # the hole deck And we can make it look better. >>> from pprint import pprint >>> pprint(deck[:10]) #52 gives the hole deck ['1 of diamonds', '1 of clubs', '1 of hearts', '1 of spades', '2 of diamonds', '2 of clubs', '2 of hearts', '2 of spades', '3 of diamonds', '3 of clubs'] >>>
I am not sure which version of Python you are using, but if you explore this code ...
... 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!
python Syntax (Toggle Plain Text)
rank = ("A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K") suit = ("c", "h", "s", "d") deck = [] for r in rank: for s in suit: card = r + s deck.append(card) print( deck )
May 'the Google' be with you!
•
•
Join Date: Aug 2008
Posts: 151
Reputation:
Solved Threads: 46
Just to explain one thing look at vegaseat code.
Does it look like this line?
This is called list List Comprehensions
I can wite it like this.
Then you see it not so differnt.
Does it look like this line?
Python Syntax (Toggle Plain Text)
deck = ['%s of %s' % (n, s) for n in numbers for s in suits]
I can wite it like this.
Python Syntax (Toggle Plain Text)
deck = [] for n in numbers: for s in suits: card = '%s of %s' % (s, n) deck.append(card)
![]() |
Similar Threads
- Shuffling a deck of cards help!? (C#)
- need help creating a "stack" of cards (C++)
- help (C++)
- Playing cards (C++)
Other Threads in the Python Forum
- Previous Thread: building pymedia
- Next Thread: Problem with Python Loop
| Thread Tools | Search this Thread |
abrupt accessdenied anti apache application approximation argv array beginner book builtin calculator change converter countpasswordentry curved dan08 dictionaries dictionary dynamic edit enter examples file float format function gui homework import inches input java keyboard lapse launcher library line lines linux list lists loop microphone mouse movingimageswithpygame mysqlquery newb number numbers numeric output parameters parsing path phonebook plugin port prime programming projects py2exe pygame pyopengl pysimplewizard python random recursion redirect remote reverse scrolledtext session simple software sprite statictext string strings syntax table terminal text textarea thread threading time tlapse trick tuple tutorial twoup ubuntu unicode unit urllib urllib2 variable wordgame wxpython






