Dec-27-2021, 12:31 AM
(This post was last modified: Dec-27-2021, 12:31 AM by BashBedlam.)
# 1 Yes you can have a global variable by the same name but I don't recommend it. # 2 Yes because number is an argument and self.number is a variable that is set to that value. # 3 chips will work correctly inside of "__init__" but nowhere else. # 4 I recommend a setter method inside of your class that contains something like "self.hand.append (argument)". # 5 Correct. # 6 Will work but would be better if "picacard" was a method inside of your Player class and called like this "self.picacard (deck). # 7 Will not work. In order to modify a global variable you would have to declare it as global inside of your class hand = 3 #(1)<---------------I can have a global variable with the same name has an instance variable deck = [['1','H'], ['2','H'], ['3','H'], ['4','H'], ['5','H']] def pickacard(deck): card = deck[0] deck.remove(deck[0]) return card class Player: flop = [] turn = [] river = ['This is Player.river'] def __init__(self, number, chips): self.number = number #(2)<---------Since it's number I need to pass an argument when I create an object of that class chips = 4#(3)<----------------Since there's no self. before this doesn't work self.hand = []#(4)<------------Since i declare it has an empty list, I don't need to give an argument when i create an object, but how do append something do this list? self.show_how_to_use_class_and_local_variables () def show_how_to_use_class_and_local_variables (self) : print ('\n') print (self.hand) print (Player.river) print ('Next is the error because "hand" is undefined.\n\n') print (hand)#<----------------This calls the global variable, not the instance variable #print(self.chips)#(5)Doesn't work because you can't declare a instance variable like this def pickplayershand(self, deck, pickacard): #<--------- Here (6) card = pickacard(deck) deck.remove(deck[0]) hand.append(card)#<--------------------And here(7) card = pickacard(deck) deck.remove(deck[0]) hand.append(card) return player1 = Player (7, 11) print(player1.hand)