| | |
Displaying Deck of Cards
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Sep 2009
Posts: 6
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
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!
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 |
Tag cloud for Python
accessdenied apache application argv beginner book change code color dictionary dynamic edit editing enter examples excel file filename float format ftp function gui homework import inches input java keyboard lapse library line lines linux list lists loop microphone mouse movingimageswithpygame mysql newb number numbers numeric output parameters parsing path port prime program programming projects py2exe pygame pyopengl pyqt python random recursion recursive redirect remote reverse rpg scrolledtext search server session simple smtp software sprite ssh statictext string strings syntax table tennis terminal text thread threading time tkinter tlapse trick tuple tutorial ubuntu unicode unit urllib urllib2 variable windows wordgame wxpython






