+ 2

Can not find a mistake.

Hello everyone, I need a help, I can not find a mistake (maybe some mistakes). This is text of task: 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. This is my code: class Player: def __init__(self, name, lives): self.name = name self._lives = lives def hit(self): #ваш код if self._lives > 0: self._lives = self._lives - 1 if self._lives <= 0: print("Game over") #print(self._lives) p = Player("Cyberpunk77", 3) p.hit() p.hit() p.hit()

9th Jan 2022, 4:17 PM
Lyapunov Alexander
Lyapunov Alexander - avatar
7 odpowiedzi
+ 3
Lyapunov Alexander , you have a typo => "Game over" should be "Game Over".
9th Jan 2022, 6:18 PM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar
+ 2
You can change the print(self._lives) to another line after the modified variable.
9th Jan 2022, 4:28 PM
FanYu
FanYu - avatar
+ 2
Yes, you are right. Line "print(self._lives)" to need just for checking. I have corrected text of program and now this line is commentary. But, could you say, where is the mistake?
9th Jan 2022, 4:40 PM
Lyapunov Alexander
Lyapunov Alexander - avatar
+ 2
Seems no if the input is limited. What's the sample input and output ? What's the wrong prompt?
9th Jan 2022, 5:05 PM
FanYu
FanYu - avatar
+ 2
Your prog does work...but there's not need to check for "<= 0", just do:- if self._lives > 0: self._lives = self._lives - 1 else: print("Game over")
9th Jan 2022, 5:34 PM
rodwynnejones
rodwynnejones - avatar
+ 2
TheWh¡teCat 🇧🇬 Exactly, this code works.
9th Jan 2022, 6:59 PM
Lyapunov Alexander
Lyapunov Alexander - avatar
+ 1
Thank you everyone, but internal test does not pass (
9th Jan 2022, 6:01 PM
Lyapunov Alexander
Lyapunov Alexander - avatar