J. Doe
J. Doe

Reputation: 11

How to create a deck of cards, have them 'shuffled' and then have two players pick the top two cards

I'm, trying to create a program where a deck of cards (30 cards in total, which each are labeleld 1-10 and assigned a color of yellow, black and red) are shuffled, and then two players choose a card from the 'top' of the deck.

so far i have this:

class Card:
def __init__(self, value, color):
    self.value = value
    self.color = color

How exactly do i 'shuffle' the deck then have two players choose the top two cards?

Upvotes: 0

Views: 583

Answers (1)

0liveradam8
0liveradam8

Reputation: 778

Try this code:

import random

class Card:
    def __init__(self, value, color):
        self.value = value
        self.color = color

#Store all 30 cards in a list
cards=[]
for i in range(0,10):
    cards.append(Card(i,"red"))
    cards.append(Card(i,"black"))
    cards.append(Card(i,"yellow"))

#Shuffle the deck
random.shuffle(cards)

#Get top card:
topCard=cards[-1]
del cards[-1]

print(str(topCard.value) + ", " + topCard.color)

Where the card that the player has removed is topCard

Upvotes: 1

Related Questions