I would like to preface this by saying that I am very new to Python and coding in general, so it's very possible that I use some terms incorrectly. I am trying to make a basic version of the card game "Exploding Kittens" in Python. I am trying to deal cards from a deck (without kittens, the card you want to avoid drawing) to an instance attribute "hand" to each object the class Player, then remove the card from the deck. My issue is that I can't make the instance attribute hand without it seeming like it acts like a class attribute. My code and result is displayed below:
import random
# Deck without kittens to deal out to the Players
deck_no_kittens = ["Attack","Attack","Attack","Attack","Steal","Steal","Steal","Steal","Favor","Favor","Favor","Favor","See the future","See the future","See the future","See the future","See the future","Alter the future","Alter the future","Shuffle","Shuffle","Shuffle","Shuffle","Skip","Skip","Skip","Skip"]
# Default starting hand for each player
start_hand = ["Defuse"]
class Player():
def __init__(self, hand, alive, knowskitten):
# Hand of each Player
self.hand = hand
# Determines if Player is alive
self.alive = True
# Determines if the Player knows if there is a kitten
self.knowskitten = False
# Defines function that deals to Player while also removing it from deck_no_kittens
def deal(player):
random_index = random.randint(0,len(deck_no_kittens)-1)
card_to_add = deck_no_kittens.pop(random_index)
player.hand.append(card_to_add)
# Initialize objects
computer1 = Player(start_hand, True, False)
computer2 = Player(start_hand, True, False)
user = Player(start_hand, True, False)
# Below is where my issue seems to be - the hand remains the same throughout each deal
# Deals 5 times, alternating, to computer1 and computer2, and prints the hand each time
for i in range(5):
deal(computer1)
print("\ncomputer1 hand is "+str(computer1.hand))
deal(computer2)
print("\ncomputer2 hand is"+str(computer2.hand))
# Prints deck_no_kittens
print("\n"+str(deck_no_kittens))
Result:
computer1 hand is ['Defuse', 'Attack']
computer2 hand is['Defuse', 'Attack', 'Skip']
computer1 hand is ['Defuse', 'Attack', 'Skip', 'See the future']
computer2 hand is['Defuse', 'Attack', 'Skip', 'See the future', 'Steal']
computer1 hand is ['Defuse', 'Attack', 'Skip', 'See the future', 'Steal', 'Skip']
computer2 hand is['Defuse', 'Attack', 'Skip', 'See the future', 'Steal', 'Skip', 'Attack']
computer1 hand is ['Defuse', 'Attack', 'Skip', 'See the future', 'Steal', 'Skip', 'Attack', 'Attack']
computer2 hand is['Defuse', 'Attack', 'Skip', 'See the future', 'Steal', 'Skip', 'Attack', 'Attack', 'Shuffle']
computer1 hand is ['Defuse', 'Attack', 'Skip', 'See the future', 'Steal', 'Skip', 'Attack', 'Attack', 'Shuffle', 'Favor']
computer2 hand is['Defuse', 'Attack', 'Skip', 'See the future', 'Steal', 'Skip', 'Attack', 'Attack', 'Shuffle', 'Favor', 'Shuffle']
['Attack', 'Steal', 'Steal', 'Steal', 'Favor', 'Favor', 'Favor', 'See the future', 'See the future', 'See the future', 'See the future', 'Alter the future', 'Alter the future', 'Shuffle', 'Shuffle', 'Skip', 'Skip']
I expected that each hand for each object would be different, but each deal adds to a universal hand. Any help/suggestions are appreciated.