Posts: 53
Threads: 14
Joined: Mar 2017
Mar-26-2017, 01:54 AM
(This post was last modified: Mar-26-2017, 01:57 AM by Liquid_Ocelot.)
Hey guys so I need to write a program that prints random cards from a list and prints it in a hand of 5 cards
card 1
card 2
card 3
card 4
card 5
I have figured out how to use the random.choice keyword but I am having a little bit of trouble with printing each card on a new line as my output just prints one random card 5 times instead of 5 random cards.....
import random
suites = ['Hearts' , 'Diamonds' , 'Spades' , 'Clubs']
cardFaces = ['Ace', 2,3,4,5,6,7,8,9,'Jack','Queen','King']
pickACard = random.choice (suites)
pickAFace = random.choice (cardFaces)
hand = []
for i in range(5): # do the body five times
card = pickACard , pickAFace
hand.append(card)
for card in hand:print(random.choice(hand)) output
('Diamonds', 'King')
('Diamonds', 'King')
('Diamonds', 'King')
('Diamonds', 'King')
('Diamonds', 'King')
Process finished with exit code 0
For instance: Write a function that returns a card (e.g. "2 of Hearts", "Jack of Diamonds")
each time you call it. e.g. to deal a five card hand:
Posts: 5,151
Threads: 396
Joined: Sep 2016
Quote:pickACard = random.choice (suites)
pickAFace = random.choice (cardFaces)
this is choosing a random element from each list once. You then are just reusing each chosen one instead of getting a new one
import random
suites = ['Hearts' , 'Diamonds' , 'Spades' , 'Clubs']
cardFaces = ['Ace', 2,3,4,5,6,7,8,9,'Jack','Queen','King']
hand = []
for i in range(5): # do the body five times
card = random.choice(suites) , random.choice(cardFaces)
hand.append(card)
for card in hand:print(random.choice(hand))
Recommended Tutorials:
Posts: 53
Threads: 14
Joined: Mar 2017
(Mar-26-2017, 02:23 AM)metulburr Wrote: Quote: pickACard = random.choice (suites) pickAFace = random.choice (cardFaces)
this is choosing a random element from each list once. You then are just reusing each chosen one instead of getting a new one import random suites = ['Hearts' , 'Diamonds' , 'Spades' , 'Clubs'] cardFaces = ['Ace', 2,3,4,5,6,7,8,9,'Jack','Queen','King'] hand = [] for i in range(5): # do the body five times card = random.choice(suites) , random.choice(cardFaces) hand.append(card) for card in hand:print(random.choice(hand))
Ah I forgot to add the random.choice in the parameters for the card variable. Cheers dude
Posts: 7,320
Threads: 123
Joined: Sep 2016
Mar-26-2017, 03:32 AM
(This post was last modified: Mar-26-2017, 03:32 AM by snippsat.)
Is better to make the deck first than choose from two list.
Indentation is 4-space in Python.
from random import shuffle
suites = ['Hearts', 'Diamonds', 'Spades', 'Clubs']
cardFaces = ['Ace', 2,3,4,5,6,7,8,9,'Jack','Queen','King']
deck = ['{0} of {1}'.format(v, s) for v in cardFaces for s in suites]
shuffle(deck)
print(deck[:5]) Output: ['8 of Clubs', '9 of Spades', '5 of Clubs', 'Jack of Spades', 'Queen of Diamonds']
Nicer in 36 with f-string.
from random import shuffle
suites = ['Hearts', 'Diamonds', 'Spades', 'Clubs']
cardFaces = ['Ace', 2,3,4,5,6,7,8,9,'Jack','Queen','King']
deck = [f'{v} of {s}' for v in cardFaces for s in suites]
shuffle(deck)
print(deck[:5])
Posts: 4,220
Threads: 97
Joined: Sep 2016
(Mar-26-2017, 03:32 AM)snippsat Wrote: Is better to make the deck first than choose from two list.
The reason it's better is that by picking randomly from the two lists, you are doing what they call "choosing with replacement." You are not removing the chance of pulling that card again. So there is a chance that you will choose the same card twice, which is not possible when dealing from a real standard deck of cards.
Posts: 7,320
Threads: 123
Joined: Sep 2016
Mar-26-2017, 12:14 PM
(This post was last modified: Mar-26-2017, 12:14 PM by snippsat.)
When i make the deck it has 52 card in order.
Using random.shuffle shuffle the deck in place.
So running the code it will never choose the same card twice when dealing out 5 cards.
Which i think solve his task.
if taking task further where used card can not be chosen again it can be a problem.
Then has to use something like list.pop to get cards out of deck.
|