Drop hier links of afbeeldingen om ze aan de editor toe te voegen.

Another modify task, and this time the random thing is not a number but an item from a list.

The editor starts you off with a program that draws one playing card:

import random

card_values = ["Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"]

card = random.choice(card_values)
print("You drew the " + card)

Run it a few times. It picks one of the thirteen values at random, but a real card has a suit as well, and a real hand has more than one card.

Your task

Change the program so that:

  1. It also picks a random suit from a new list of the four suits.
  2. It includes the suit in the output message.
  3. It uses a loop to draw 5 cards, and adds each one to a new list as it is picked.
  4. It outputs the list of 5 cards once all of them have been picked.

Some details, so that your program can be checked automatically:

The output

Five lines, one for each card as it is drawn, where VALUE is the value and SUIT is the suit:

You drew the VALUE of SUIT

Then one more line: the whole list of five cards, printed in one go, with its square brackets, quotes and commas.

Example

The cards are random, so your program will draw a different hand every time you run it. This is one possible run:

Output:

You drew the Three of Hearts
You drew the Five of Hearts
You drew the Eight of Spades
You drew the Eight of Spades
You drew the King of Diamonds
['Three of Hearts', 'Five of Hearts', 'Eight of Spades', 'Eight of Spades', 'King of Diamonds']

The Eight of Spades comes up twice. That is allowed: random.choice does not take anything out of the list, so every card is drawn from a full deck.

How can a random program be checked?

The checks fix the random seed before your program runs, so they know which cards it is going to draw. That only works if your program asks for random items in the same order as the checks expect: first a value, then a suit, five times over. Pick the suit first and your hand comes out different, even though there is nothing wrong with it as a game.

Help! My code doesn't work!

Make sure that you check for the following things:

  • The list of suits reads "Hearts", "Diamonds", "Clubs", "Spades", in that order, with capital letters.
  • The value is picked before the suit, inside the loop.
  • The new list is created before the loop, as an empty list [].
  • The card is added to the list inside the loop, with append.
  • The spaces around of come from inside the speech marks: " of ".
  • The print of the list is not indented, so it runs once, after the loop.