+ 2
Why two different instances print same output?
This code simulates black jack game. The Card class gets properly random from parent Deck, but problem is the instantiated two different Hand objects prints the same output, that is problem because the hands switch in for-each loop but act like only one object is parent for reference of another object. They should represent different cards in cards collection. https://code.sololearn.com/cFnp7watcx4g/?ref=app
2 Respuestas
+ 6
The "cards" list (line 22) is a class variable, not an instance variable. This means such list is shared by all instances of the class.
Once you modify the list when you create your first intance of class Hand class, it stays the same for all instances you create in the future.
You can correct this by making the "cards" list an instance variable:
class Hand:
def __init__(self):
self._score = 0
self.cards = []
And to improve the output of your code change lines 57-59 for this:
for handResults in self.players:
handResults.showCards()
print("Score:", handResults.getScore())
print()
+ 1
This solved the problem, your answer was very helpful.
Thank You!