Home > Blockchain >  i don't know what to change about this code. does anyone know why it keeps giving me the same T
i don't know what to change about this code. does anyone know why it keeps giving me the same T

Time:01-08

this is the error that i keep getting: Traceback (most recent call last): File "C:\Users\baris\PycharmProjects\pythonProject2\main.py", line 60, in print(my_hand) TypeError: str returned non-string (type NoneType)

this is the complete code but the problem i run in to is that it doesn't add anything to my_hand. does anyone know why?:

class Card(object):
    RANKS = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"]
    SUITS = ["D", "S", "H", "C"]

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

    def __str__(self):
        rep = self.rank   self.suit
        return str(rep)


class Hand(object):
    """a hand of playing cards"""
    def __init__(self):
        self.cards = []

    def __str__(self):
        if self.cards:
            rep = ""
            for card in self.cards:
                rep  = str(card)   "  "
        else:
            rep = "<empty>"
            return rep

    def clear(self):
        self.cards = []

    def add(self, card):
        self.cards.append(card)

    def give(self, card, other_hand):
        self.cards.remove(card)
        other_hand.add(card)


card1 = Card(rank="A", suit="c")
print(card1)
card2 = Card(rank="2", suit="c")
card3 = Card(rank="3", suit="c")
card4 = Card(rank="4", suit="c")
card5 = Card(rank="5", suit="c")
print(card2)
print(card3)
print(card4)
print(card5)

my_hand = Hand()
print("my hand before i add cards\n", my_hand)
my_hand.add(card1)
my_hand.add(card2)
my_hand.add(card3)
my_hand.add(card4)
my_hand.add(card5)
print("my hand after adding 5 cards: ")
print(my_hand)

your_hand = Hand()
my_hand.give(card1, your_hand)
my_hand.give(card2, your_hand)
print("your hand: ")
print(your_hand)
print("my hand: ")
print(my_hand)

my_hand.clear()
print("my hand now: ")
print(my_hand)

CodePudding user response:

In the __str__ function of Hand, you aren't returning rep when self.cards has anything, so the code just falls through to the end of the function. Adding return rep after the loop should fix it.

  • Related