473,395 Members | 2,253 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

How to create a deck of cards

Hi there. Thanks for your help.
I am experimenting with lists and dictionaries and would like to make a dictionary of playing cards as my eventual outcome.
I know I can write it out by hand but this is an excercise so I can learn about data etc. (this is not homework, I'm just learning python.

What I have accomplished so far.
I have managed to make a nested list of the name of the card, and its associated value. i.e. [['2 of hearts', 2] etc..

Here is my code.
I would like to know if there are any suggestions to improve upon it.
Thanks
Miguel
PS I know that the Ace has more than one value but I will work on that soon enough.

Expand|Select|Wrap|Line Numbers
  1. ##Declare a basic list of items that all cards will have
  2. cards = [['2',2], ['3',3], ['4',4],
  3.             ['5',5], ['6',6], ['7',7], ['8',8], ['9',9],
  4.             ['10',10], ['Jack',10], ['Queen',10], ['King',10], ['Ace',11]]
  5.  
  6. ##declare a separate list for each suit
  7. hearts = []
  8. clubs = []
  9. spades =[]
  10. diamonds =[]
  11.  
  12. ##Now create the list of cards for each suit
  13. for x,y in cards:
  14.     hearts_row=[]
  15.     clubs_row=[]
  16.     spades_row=[]
  17.     diamonds_row=[]
  18.     for item in cards[0][0]:
  19.         hearts_row.append(x + ' of hearts')
  20.         hearts_row.append(y)
  21.  
  22.         clubs_row.append(x + ' of clubs')
  23.         clubs_row.append(y)
  24.  
  25.         spades_row.append(x + ' of spades')
  26.         spades_row.append(y)
  27.  
  28.         diamonds_row.append(x + ' of diamonds')
  29.         diamonds_row.append(y)
  30.  
  31.     hearts.append(hearts_row)
  32.     clubs.append(hearts_row)
  33.     spades.append(hearts_row)
  34.     diamonds.append(hearts_row)
  35.  
  36.  
  37. ##combine all suits into one deck
  38.  
  39.  
  40. deck = hearts + clubs + spades + diamonds
  41. ##Print Deck
  42. print (deck)
  43.  
  44.  
Here is my output!

[['2 of hearts', 2], ['3 of hearts', 3], ['4 of hearts', 4], ['5 of hearts', 5], ['6 of hearts', 6], ['7 of hearts', 7], ['8 of hearts', 8], ['9 of hearts', 9], ['10 of hearts', 10], ['Jack of hearts', 10], ['Queen of hearts', 10], ['King of hearts', 10], ['Ace of hearts', 11], ['2 of hearts', 2], ['3 of hearts', 3], ['4 of hearts', 4], ['5 of hearts', 5], ['6 of hearts', 6], ['7 of hearts', 7], ['8 of hearts', 8], ['9 of hearts', 9], ['10 of hearts', 10], ['Jack of hearts', 10], ['Queen of hearts', 10], ['King of hearts', 10], ['Ace of hearts', 11], ['2 of hearts', 2], ['3 of hearts', 3], ['4 of hearts', 4], ['5 of hearts', 5], ['6 of hearts', 6], ['7 of hearts', 7], ['8 of hearts', 8], ['9 of hearts', 9], ['10 of hearts', 10], ['Jack of hearts', 10], ['Queen of hearts', 10], ['King of hearts', 10], ['Ace of hearts', 11], ['2 of hearts', 2], ['3 of hearts', 3], ['4 of hearts', 4], ['5 of hearts', 5], ['6 of hearts', 6], ['7 of hearts', 7], ['8 of hearts', 8], ['9 of hearts', 9], ['10 of hearts', 10], ['Jack of hearts', 10], ['Queen of hearts', 10], ['King of hearts', 10], ['Ace of hearts', 11]]
Sep 12 '14 #1

✓ answered by dlite922

Here you go buddy, this is the easier way of doing it.

I'll walk you through the thought process:

1. First outline what you have: Card values, card suites:

Expand|Select|Wrap|Line Numbers
  1. card_values = ['2','3','4','5','6','7','8','9','10','Jack','Queen','King','Ace']
  2. card_suites = ['Hearts', 'Clubs', 'Diomands', 'Spades']
  3.  
2. Then you need to combine these lists (multiply them), but still leave them in a list. You know you need to loop both lists, which means you have two for-loops. In python, list comprehension is a great way to do it. It's hard to understand at first (it was for me) but eventually you'll get it the more you practice it.

Expand|Select|Wrap|Line Numbers
  1. result = [[v + ' of ' + s,v] for s in card_suites for v in card_values]
  2.  
Your output in result looks like this:

Expand|Select|Wrap|Line Numbers
  1. [[v + ' of ' + s,v] for s in card_suites for v in card_values]
  2. [['2 of Hearts', '2'], ['3 of Hearts', '3'], ['4 of Hearts', '4'], ['5 of Hearts', '5'], ['6 of Hearts', '6'], ['7 of Hearts', '7'], ['8 of Hearts', '8'], ['9 of Hearts', '9'], ['10 of Hearts', '10'], ['Jack of Hearts', 'Jack'], ['Queen of Hearts', 'Queen'], ['King of Hearts', 'King'], ['Ace of Hearts', 'Ace'], ['2 of Clubs', '2'], ['3 of Clubs', '3'], ['4 of Clubs', '4'], ['5 of Clubs', '5'], ['6 of Clubs', '6'], ['7 of Clubs', '7'], ['8 of Clubs', '8'], ['9 of Clubs', '9'], ['10 of Clubs', '10'], ['Jack of Clubs', 'Jack'], ['Queen of Clubs', 'Queen'], ['King of Clubs', 'King'], ['Ace of Clubs', 'Ace'], ['2 of Diomands', '2'], ['3 of Diomands', '3'], ['4 of Diomands', '4'], ['5 of Diomands', '5'], ['6 of Diomands', '6'], ['7 of Diomands', '7'], ['8 of Diomands', '8'], ['9 of Diomands', '9'], ['10 of Diomands', '10'], ['Jack of Diomands', 'Jack'], ['Queen of Diomands', 'Queen'], ['King of Diomands', 'King'], ['Ace of Diomands', 'Ace'], ['2 of Spades', '2'], ['3 of Spades', '3'], ['4 of Spades', '4'], ['5 of Spades', '5'], ['6 of Spades', '6'], ['7 of Spades', '7'], ['8 of Spades', '8'], ['9 of Spades', '9'], ['10 of Spades', '10'], ['Jack of Spades', 'Jack'], ['Queen of Spades', 'Queen'], ['King of Spades', 'King'], ['Ace of Spades', 'Ace']]
  3.  
The whole thing in only three lines of code!
Expand|Select|Wrap|Line Numbers
  1. values = ['2','3','4','5','6','7','8','9','10','Jack','Queen','King','Ace']
  2. suites = ['Hearts', 'Clubs', 'Diomands', 'Spades']
  3. deck = [[v + ' of ' + s,v] for s in suites for v in values]
  4.  
You might say you're missing the values for Jack Queen and King! Ah! I'll leave this as an exercise to you, but if you can't figure it out, I'll help you here or post another question. HINT: Turn the value list into a dict.

2 11396
dlite922
1,584 Expert 1GB
Here you go buddy, this is the easier way of doing it.

I'll walk you through the thought process:

1. First outline what you have: Card values, card suites:

Expand|Select|Wrap|Line Numbers
  1. card_values = ['2','3','4','5','6','7','8','9','10','Jack','Queen','King','Ace']
  2. card_suites = ['Hearts', 'Clubs', 'Diomands', 'Spades']
  3.  
2. Then you need to combine these lists (multiply them), but still leave them in a list. You know you need to loop both lists, which means you have two for-loops. In python, list comprehension is a great way to do it. It's hard to understand at first (it was for me) but eventually you'll get it the more you practice it.

Expand|Select|Wrap|Line Numbers
  1. result = [[v + ' of ' + s,v] for s in card_suites for v in card_values]
  2.  
Your output in result looks like this:

Expand|Select|Wrap|Line Numbers
  1. [[v + ' of ' + s,v] for s in card_suites for v in card_values]
  2. [['2 of Hearts', '2'], ['3 of Hearts', '3'], ['4 of Hearts', '4'], ['5 of Hearts', '5'], ['6 of Hearts', '6'], ['7 of Hearts', '7'], ['8 of Hearts', '8'], ['9 of Hearts', '9'], ['10 of Hearts', '10'], ['Jack of Hearts', 'Jack'], ['Queen of Hearts', 'Queen'], ['King of Hearts', 'King'], ['Ace of Hearts', 'Ace'], ['2 of Clubs', '2'], ['3 of Clubs', '3'], ['4 of Clubs', '4'], ['5 of Clubs', '5'], ['6 of Clubs', '6'], ['7 of Clubs', '7'], ['8 of Clubs', '8'], ['9 of Clubs', '9'], ['10 of Clubs', '10'], ['Jack of Clubs', 'Jack'], ['Queen of Clubs', 'Queen'], ['King of Clubs', 'King'], ['Ace of Clubs', 'Ace'], ['2 of Diomands', '2'], ['3 of Diomands', '3'], ['4 of Diomands', '4'], ['5 of Diomands', '5'], ['6 of Diomands', '6'], ['7 of Diomands', '7'], ['8 of Diomands', '8'], ['9 of Diomands', '9'], ['10 of Diomands', '10'], ['Jack of Diomands', 'Jack'], ['Queen of Diomands', 'Queen'], ['King of Diomands', 'King'], ['Ace of Diomands', 'Ace'], ['2 of Spades', '2'], ['3 of Spades', '3'], ['4 of Spades', '4'], ['5 of Spades', '5'], ['6 of Spades', '6'], ['7 of Spades', '7'], ['8 of Spades', '8'], ['9 of Spades', '9'], ['10 of Spades', '10'], ['Jack of Spades', 'Jack'], ['Queen of Spades', 'Queen'], ['King of Spades', 'King'], ['Ace of Spades', 'Ace']]
  3.  
The whole thing in only three lines of code!
Expand|Select|Wrap|Line Numbers
  1. values = ['2','3','4','5','6','7','8','9','10','Jack','Queen','King','Ace']
  2. suites = ['Hearts', 'Clubs', 'Diomands', 'Spades']
  3. deck = [[v + ' of ' + s,v] for s in suites for v in values]
  4.  
You might say you're missing the values for Jack Queen and King! Ah! I'll leave this as an exercise to you, but if you can't figure it out, I'll help you here or post another question. HINT: Turn the value list into a dict.
Sep 12 '14 #2
Thanks so much dlite922. I feel like an amateur asking these questions and it's nice to see that python can do this with such little code.
Miguel
Sep 15 '14 #3

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

Similar topics

23
by: JC | last post by:
I am very new to programming and learning on my own. Why do I keep getting duplicate values using this code? I want to shuffle a deck of 52 cards. The logic seems right to me. Randomize For...
26
by: Radith | last post by:
Hi All, I have got this task which I cant get my head around: It asks how I would store information: I just want to get an opinion from the group: So here's the question: " You have been...
6
by: CaseyB | last post by:
If I wanted to create a game like Solitaire that would first randomly shuffle a deck of cards, I figured out that all I had to use is the Random() class or rnd and make sure I use the Randomize...
11
by: cazconv2007 | last post by:
hi i have homework due tomorrow been trying to do java homework all week and nothings happening really starting to stress out i dont understand what the program is meant to do or how to use three...
8
by: garyrowell | last post by:
I have been at this programme for hours trying to work out what is wrong. Any help would be very much appricated. Here is the breif I received. The program This week you are going to write three...
1
by: Vneha | last post by:
import java.util.*; public class Deck { public static int numSuits = 4; public static int numRanks = 13; public static int numCards = numSuits * numRanks; private Card...
1
by: dr4g0nk1ng | last post by:
i have this class that should create an array of cards public class DeckOfCards{ public Card card; public Card deck; DeckOfCards() { for (int s = 0; s < 3; s++)
21
by: blehhhhh | last post by:
So I'm trying to work on this problem of deck of cards wherein I have a few classes along with a tester class. Everything else is done. I just need help with implementing the methods properly. Here's...
0
by: markneidert | last post by:
I am looking for a program to creat flashcards intended to sell. WinFlash Educator is perfect but, it doesn't produce flash cards that are protected from use by people who didn't purchase them. I...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.