0

I'm trying to create a game of Kings Cup (drinking game that involves cards) with Python. As it stands at the moment, when I print the deck and participants lists, it appears that the console is print out memory addresses. I would appreciate any help, pointers, tips, suggestions. Please and thank you.

import random

class Participant:
    def __init__(self, name, gender):
        self.name = name
        self.gender = gender
        self.cards = []
    def drawCard(self):
        self.cards += deck.pop()
    def showCards(self):
        print(self.name) 
        print(self.cards) 


class Card:
    def __init__(self, value, suite):
        self.value = value
        self.suite = suite
    def getValue(self):
        return self.value
    def getSuite(self):
        return self.suite
    def __str__(self):
        return ("{0} of {1}".format(self.value, self.suite))

deck = []
participants = []

def shuffleDeck():
    for suite in ["Clubs", "Diamonds", "Hearts", "Spades"]:
        for num in ["Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"]:
        deck.append(Card(num, suite))
    random.shuffle(deck)
    return deck 


players = input("Enter the number of players: ")
numPlayers = int(players)
if numPlayers < 3:
    print(players)
    print("Not enough players.")
elif numPlayers > 12:
    print(players)
    print("That's too many players.")
else:
    for player in range(numPlayers):
        player += 1
        name = input("Player %s's Name: " %player) 
        gender = input("Player %s's Gender: " %player)
        participants.append(Participant(name, gender))

deck = shuffleDeck()

print(deck)
print(participants)
5
  • 2
    You just need to define a __repr__ as well as a __str__. Or, alternatively, don’t try to print lists of Cards, print each Card individually, because lists always show the repr of their contents, not the str, because lists aren’t really meant for “human” (as opposed to programmer) consumption. Commented Aug 23, 2018 at 23:12
  • 2
    Typically, your repr should look like Card("2", "Hearts"). In other words, like a Python expression that would create an equal object if you pasted it into the interactive interpreter, because you normally only print the repr out for debugging purposes. Commented Aug 23, 2018 at 23:15
  • 1
    Your indentation is off in the append shuffleDeck function. The append method needs to be nested in the second loop. Commented Aug 23, 2018 at 23:20
  • Thanks guys. I was only printing out the entire deck to testing purposes. My code is still a work in progress. I appreciate your guys' help. Commented Aug 23, 2018 at 23:25
  • @nicholishen You're right. Thanks for that! Commented Aug 23, 2018 at 23:29

2 Answers 2

4

Define a representation for each instance. For example:

class Card:
    def __init__(self, value, suite):
        self.value = value
        self.suite = suite
    def getValue(self):
        return self.value
    def getSuite(self):
        return self.suite
    def __str__(self):
        return ("{0} of {1}".format(self.value, self.suite))
    def __repr__(self):
        return ("{0} of {1}".format(self.value, self.suite))

Then

>> a = [Card(3,'Diamonds'),Card(2,'Spades')]
>> print(a)
[3 of Diamonds, 2 of Spades]

That said, see @abarnerts comments about formatting repr

Sign up to request clarification or add additional context in comments.

Comments

1

Python stores information in memory with pointers. When you create a variable, you say: Hey python, remember that this variable points to this piece of memory.

When you print a string or a number, python will auto format it for you, but when more complex objects are involved, it doesn’t know what to do.

To inform python what to do, you need to create the magic function __str__ (2 underscores on each sides) which you already did.

The problem is, when you print a list, you don’t print each individual objects, you print what the list represents of each objects. It’s more useful like that, because you’re not storing simple objects like strings or ints, but complex and defined structures.

To tell python what to represent, you need to create the magic function __repr__. I strongly suggest that you do not copy the __str__ function. It would be preferred to print something like Card(*value*, *suite*). In other words: your init function.

Gift code:

def __repr__(self):
    return '{}({}, {})'.format(self.__name__, self.value, self.suite)

5 Comments

strings are objects. Classes are always involved.
Sorry, I didn’t word it correctly, what I meant is: “but when non-default objects are involved, python doesn’t know what to do”. I’ll edit that.
I think the phrase is user-defined classes.
It also applies to non-user defined classes, but I guess this is what it concerns, so yeah.
Well, essentially, you are inheriting object.__repr__ if you do t define your own (or inherit another)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.