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.
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]]
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.
Code:
##Declare a basic list of items that all cards will have
cards = [['2',2], ['3',3], ['4',4],
['5',5], ['6',6], ['7',7], ['8',8], ['9',9],
['10',10], ['Jack',10], ['Queen',10], ['King',10], ['Ace',11]]
##declare a separate list for each suit
hearts = []
clubs = []
spades =[]
diamonds =[]
##Now create the list of cards for each suit
for x,y in cards:
hearts_row=[]
clubs_row=[]
spades_row=[]
diamonds_row=[]
for item in cards[0][0]:
hearts_row.append(x + ' of hearts')
hearts_row.append(y)
clubs_row.append(x + ' of clubs')
clubs_row.append(y)
spades_row.append(x + ' of spades')
spades_row.append(y)
diamonds_row.append(x + ' of diamonds')
diamonds_row.append(y)
hearts.append(hearts_row)
clubs.append(hearts_row)
spades.append(hearts_row)
diamonds.append(hearts_row)
##combine all suits into one deck
deck = hearts + clubs + spades + diamonds
##Print Deck
print (deck)
[['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]]
Comment