How to debug OOP Class/Method in Python -
i'm attempting canonical pedagogical programming exercise of building program simulate card game. how far am:
class card: def __init__(self, suit, rank): self.suit = suit self.rank = rank self.card = str(self.suit) + " " + str(self.rank) class deck: def __init__(self): self.deck = [] def deck_maker(self): ranks = range(1, 11) + ["j", "q", "k", "a"] suits = ["club", "heart", "spade", "diamond"] return self.deck.append(card(suits[0], ranks[0]))
obviously i'm not done, have loop on suits , ranks construct deck. however, before continuing want check i'm on track. want print results after invoking deck_maker method. have attempted below code...
def __str__(self): d = self.deck d2 = d.deck_maker() return str(d2)
unfortunately, print(deck().deck_maker())
returns none
.
what doing wrong? more generally, how programmers poke around within classes , methods while make them?
thanks!
the append method on list returns none expected. try splitting lines:
deck = deck() deck.deck_maker() print(deck)
or change method deck_maker be:
def deck_maker(self): ranks = range(1, 11) + ["j", "q", "k", "a"] suits = ["club", "heart", "spade", "diamond"] self.deck.append(card(suits[0], ranks[0])) return self
edit: there problem in str method. when set d self.deck setting list. list doesn't know method deck_maker. try changing method below. turn list string. make more readable, want add str method card class.
def __str__(self): return str(self.deck)
Comments
Post a Comment