Creating a deck of cards
Another kind of random event that's used a lot in games is drawing a card. It's random because the deck is shuffled, so you don't know what card is coming next. And every time the deck is shuffled, the order is different.
With dice and coin tosses, we said that every toss has the same probability, because the coin (or die) has no memory. But that's not true with cards. As you draw cards from the deck, there are fewer and fewer cards left (in most games, anyway). That changes the probability of drawing each one of the remaining cards.
For example, when you start with a full deck, the chances of drawing the 4 of Hearts is 1/52, or about 2 percent. This is because there are 52 cards in the deck, and only one 4 of Hearts. If you keep drawing (and haven't drawn the 4 of Hearts yet), when you're halfway through the deck, the chances of getting the 4 of Hearts are 1/26, or about 4 percent. By the time you reach the last card, if you still haven't drawn the 4 of Hearts, the chances of drawing it are 1/1, or 100 percent. It's certain that you'll draw the 4 of Hearts next, because it's the only card left.
The reason I'm telling you all this is to show that, if we're going to make a computer game using a deck of cards, we need to keep track of which cards have been removed from the deck as we go along. One good way to do this is with a list. We can start with a list of all 52 cards in the deck and use the random.choice() function to pick cards randomly from the list. As we pick each card, we can remove it from the list (the deck) using remove().
Responses

Jasmin H2 years ago
 Reply

keiren1 year ago
 Reply