This post has been de-listed (Author was flagged for spam)
It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.
1
Help with pop() function for card game
Post Body
I'm trying to make a simple card game with a Card class and a Deck class that is composed of Card() objects. One of the functions in the Deck class is deal() which takes a specified number of cards from the deck and uses them to create a new Deck() instance. However the pop() function does not seem to reduce the size of the deck and I'm not sure why, even when called only a single time in the pick_top() function. Hopefully someone can look over my code and tell me why. Thanks
class Card:
"""Represents a card that would be used in a deck of cards."""
VALUES = {
"2" : "Two",
"3" : "Three",
"4" : "Four",
"5" : "Five",
"6" : "Six",
"7" : "Seven",
"8" : "Eight",
"9" : "Nine",
"10" : "Ten",
"J" : "Jack",
"Q" : "Queen",
"K" : "King",
"A" : "Ace"}
SUITS = {
"hearts" : "Hearts",
"diamonds" : "Diamonds",
"clubs" : "Clubs",
"spades" : "Spades"}
def __init__(self, value, suit):
self.value = value
self.suit = suit
def __str__(self):
return f"{self.VALUES[self.value]} of {self.SUITS[self.suit]}"
import random
from card import Card
class Deck:
"""Represents a deck of playing cards."""
STANDARD_DECK = (Card(value, suit) for value in Card.VALUES.keys() for suit in Card.SUITS.keys())
EMPTY_DECK = ()
def __init__(self, cards = STANDARD_DECK):
"""Creates deck of cards. First card in 'cards' attribute is top of the deck, last card is the bottom."""
self.cards = list(cards)
self.card_count = len(self.cards)
def __str__(self):
"""String representation of a DeckOfCards."""
string = ""
for card in self.cards:
string = (str(card) "\n")
return string
def shuffle(self):
"""Shuffles the deck."""
random.shuffle(self.cards)
def pick_top(self):
"""Top card is taken from deck."""
top_card = self.cards.pop(0)
return top_card
def deal(self, hand_size):
"""Deals a new deck of cards of given size from top of deck."""
cards_taken = [self.pick_top() for card in range(hand_size)]
new_deck = Deck(cards_taken)
return new_deck
Test Code:
my_deck = Deck()
print(my_deck.card_count)
top_card = my_deck.pick_top
print(my_deck.card_count)
new_deck = my_deck.deal(26)
print(my_deck.card_count)
print(new_deck.card_count)
Output:
52
52
52
26
Author
Account Strength
0%
Account Age
6 years
Verified Email
Yes
Verified Flair
No
Total Karma
1,962
Link Karma
929
Comment Karma
1,015
Profile updated: 3 weeks ago
Posts updated: 2 weeks ago
Subreddit
Post Details
We try to extract some basic information from the post title. This is not
always successful or accurate, please use your best judgement and compare
these values to the post title and body for confirmation.
- Posted
- 2 years ago
- Reddit URL
- View post on reddit.com
- External URL
- reddit.com/r/learnpython...