1

I am trying to learn python and am trying to create a blackjack game. I have created a Person class in which it has an instance variable called self.hand. self.hand is a an empty list which is supposed to hold 2 initial cards and from there on any player who needs a card will receive a card through the append method. However this isn't working for me for some reason. Someone please help its been bothering me for days now!!

I have tried already creating the get_add_card (get additional card) an instance method and tried to manually append a new card object to the persons hand list. Nothing has worked.

The first chunk of code is the Person class. In that class I define self.hand as a list which would initially store 2 card objects (get_cards). After that I made another method which should append another card to the list if needed. The second block of code should check who needs a card and then append a random card to that specific persons list.

class Person:
    def __init__(self, name):
        self.name = name
        self.hand = []
        self.get_cards()

    def get_cards(self):
        for i in range(2):
            rand_card = random.choice(Deck.cards)
            self.hand.append(rand_card)
            Deck.cards.remove(rand_card)

    def get_add_card(self):
        self.hand.append(random.choice(Deck.cards))

need_card = True
while need_card:
    answer = input("Does anyone need a card? Yes or No")
    if answer.lower() == 'no':
        need_card = False
    elif answer.lower() == 'yes':
        player_need = input("Which player needs a card?").lower()
        Person(player_need).get_add_card()
        print (Person(player_need).hand)
    else:
        print("Please answer using yes or no")

The code should append a random card into the persons hand list. However that is not happening and when I try to print out their hand at the end it only shows that their are the two cards which are the cards that the person started off with in the begining.

8
  • Please indent your code correctly. Commented Mar 24, 2019 at 23:01
  • Add backticks (```) before and after your code block. Also, why is there a player_need and a need_player? Commented Mar 24, 2019 at 23:03
  • 1
    @gmds, no, do NOT add backticks. Just add spaces, as needed. Commented Mar 24, 2019 at 23:04
  • i just indented, please check it out now Commented Mar 24, 2019 at 23:05
  • You need to indent all the code by an extra four spaces. The {} button in the editor will do it for you if you select all the code after you've pasted it in. Commented Mar 24, 2019 at 23:05

1 Answer 1

1

Your problem is that every time you do Person(need_player), you're creating a separate Person object. Even if you use the same name, it's not the same object as before, and it will have a separate list as its hand attribute.

To avoid recreating your players over and over, you should create them up front and put them into a list or dictionary:

# up front, create the players (perhaps by prompting, but here hard-coded)
players = {"alice": Person("Alice"), "aob": Person("Bob")}

# later, you can look them up by name:
player_need = input("Which player needs a card?").lower()
players[player_need].get_add_card()
print(players[player_need]).hand)

You will probably need some more logic in there to avoid errors if the user enters an unknown name, but this should give you the general idea.

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

Comments

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.