IDLE
.
def create_deck(suits, values):
create an empty list
for suit in suits:
for value in values:
set card to a string consisting of the value, "of" and suit
append card to the list
return the list
def shuffle(deck):
create an empty list
while there are still entries in deck
set index to a randomly chosen index in deck
remove
get the card deck[index] and remove it
append this card to the list
return the list
def deal_hand(deck):
create an empty list
for index in deck 5 times:
get the card deck[index] and remove it
append this card to the list
return the list
random.seed(79) suits = ["Spades", "Hearts", "Diamonds", "Clubs"] values = ["Ace", "King", "Queen", "Jack", "10", "9", "8", "7", "6", "5", "4", "3", "2"] deck = create_deck(suits, values) deck = shuffle(deck) print(deal_hand(deck)) print(deal_hand(deck))
['Ace of Clubs', '9 of Clubs', '6 of Clubs', 'Queen of Diamonds', 'Ace of Spades'] ['6 of Spades', '5 of Hearts', '4 of Clubs', 'King of Clubs', '2 of Hearts']
pass
. pass
statement from create_deck. print
statement.
Write a for
loop that iterates through the list suits. print
statement. for
loop write another for
loop that
iterates through values. pass
statement fromshuffle. while
loop that runs as long as their are cards in deck. random.randrange
to do this. randrange
should be len(deck)
. while
where loop is the condition is the length of the list
deck. randrange
on the length of deck. while
loop, print shuffled. print
statement. print
statement. for
loop that runs 5 times. for
loop set the variable card to
the value you get by calling pop on deck. print
statement. print
statement. ['Ace of Clubs', '9 of Clubs', '6 of Clubs', 'Queen of Diamonds', 'Ace of Spades'] ['6 of Spades', '5 of Hearts', '4 of Clubs', 'King of Clubs', '2 of Hearts']
cd it116/hw/hw11
python3 hw11.py
['Ace of Clubs', '9 of Clubs', '6 of Clubs', 'Queen of Diamonds', 'Ace of Spades'] ['6 of Spades', '5 of Hearts', '4 of Clubs', 'King of Clubs', '2 of Hearts']
Copyright © 2020 Glenn Hoffman. All rights reserved. May not be reproduced without permission.