Python Class Method Call

Asked

Viewed 571 times

0

I’m doing a work of a playing card and I’m having a problem calling the class methods Deck(), I tried to redo some parts of the code and sometimes as an undefined object, I redo it again and still nothing, someone could give me a help if possible ? thank you in advance.

Follows the code:

  import random
class Card:
    def __init__(self, rank, suit):
        self.rank = rank
        self.suit = suit

    def getRankeado(self):
        return self.rank

    def getSuit(self):
         return self.suit


class Deck:
    ranks = {'2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A'}

    suits = {'ZAP', 'COPAS', 'OURO', 'ESPADA'}

    def __init__(self):
        self.deck = []

        for suit in Deck.suits:
            for rank in Deck.ranks:
                self.deck.append(Card(rank, suit))

    def dealCard(self):
        return self.deck.pop()

    def __repr__(self):
        return self.__repr()

    def lenDeck(self):
        return self.len(Deck)

    def shuffle(self):
        random.shuffle(self.deck)

    def imprime(self):
        return print(self.Deck, self.Card)

#saidas

c = Deck()
c.shuffle()
c2 = c.dealCard()
c2.getRank(), c2.getSuit()
  • And what’s the problem?

  • On leaving the program, I made a few more changes to the code

  • Solved? if yes publish the solution.

  • I still can not solve, when I pass getTank() and getSuit() on top of object C2 it does not return me anything.

1 answer

1

You noticed that method getRank() was spelled as getRankeado()? After the correction your program starts working correctly:

import random

class Card(object):

    def __init__(self, rank, suit):
        self.rank = rank
        self.suit = suit

    def getRank(self):
        return self.rank

    def getSuit(self):
         return self.suit

# ... Class Deck() ...

c = Deck()
c.shuffle()
c2 = c.dealCard()

print(c2.getRank(), c2.getSuit())

I added this print in the last line to display the values of the chosen card.

  • Good afternoon Giovanni, sorry for the delay in answering, I was able to solve the problem, the problem was that I was only taking the reference of the object because the Repr method was wrong and I made the changes you gave me too, thanks for the attention and the tips.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.