Need help with Data Hiding Code Coach | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Need help with Data Hiding Code Coach

I'm working on the code coach for data hiding. Here's the prompt: We are working on a game. Our Player class has name and private _lives attributes. The hit() method should decrease the lives of the player by 1. In case the lives equal to 0, it should output "Game Over". Complete the hit() method to make the program work as expected. And here's my code so far: class Player: def __init__(self, name, lives): self.name = name self._lives = lives def hit(self): if self._lives == 0: print("Game Over") else: self._lives = self.lives - 1 p = Player("Cyberpunk77", 3) p.hit() p.hit() p.hit() It never outputs anything. Can someone tell me how to correct this? Thanks in advance

12th Nov 2021, 1:08 AM
Alex
Alex - avatar
2 Answers
+ 3
In the hit() method first reduce self._lives by one, then check if self._lives == 0.
12th Nov 2021, 1:34 AM
Simon Sauter
Simon Sauter - avatar
+ 1
Thanks, that works!
12th Nov 2021, 1:39 AM
Alex
Alex - avatar