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.
player_needand aneed_player?{}button in the editor will do it for you if you select all the code after you've pasted it in.