the-honk/school/gcse/year 11/Card Shuffler.py

43 lines
1.2 KiB
Python
Raw Permalink Normal View History

2024-10-09 17:02:42 +00:00
import random
class Deck():
def __init__(self, suits):
self.cards = []
self.suits = suits
def generate(self):
for suit in self.suits:
for cardType in range(1, 14):
formattedCard = ""
if cardType == 1:
formattedCard += "Ace"
elif cardType == 11:
formattedCard += "Jack"
elif cardType == 12:
formattedCard += "Queen"
elif cardType == 13:
formattedCard += 'King'
else:
formattedCard += str(cardType)
formattedCard += ' of %s' % (suit)
self.cards.append(formattedCard)
def shuffle(self):
currentCards = self.cards
shuffledCards = []
while len(currentCards) > 0:
pick = random.randint(0, len(currentCards) - 1)
removedCard = currentCards.pop(pick)
shuffledCards.append(removedCard)
self.cards = shuffledCards
deck = Deck(["Spades", "Hearts", "Diamonds", "Clubs"])
print(deck.cards)
deck.generate()
print(deck.cards)
deck.shuffle()
print(deck.cards)