Python Forum
Choosing a random variable from 2 lists
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Choosing a random variable from 2 lists
#1
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:
Reply
#2
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:
Reply
#3
(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
Reply
#4
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])
Reply
#5
(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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#6
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.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020