0

I think mine is Ok, the results are 2 1 and game over. Why is this still wrong?

class Player: def __init__(self, name, lives): self.name = name self._lives = lives def hit(self): #your code goes here d = self._lives while d>0: d -= 1 if d == 0: print("Game Over") else: print(d) p = Player("Cyberpunk77", 3) p.hit() p.hit() p.hit()

11th Feb 2021, 7:40 AM
Ratna13
Ratna13 - avatar
8 odpowiedzi
+ 5
Ratna13 Try this: class Player: def __init__(self, name, lives): self.name = name self._lives = lives def hit(self): #your code goes here self._lives -=1 if self._lives== 0: print("Game Over") else: print(self._lives) p = Player("Cyberpunk77", 3) p.hit() p.hit() p.hit() Your while loop was the biggest problem
11th Feb 2021, 9:04 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 3
What do you mean by wrong? What is your expected output? Is this a challenge that has test cases? Please give more details for what should be the output so we may help. Thanks.
11th Feb 2021, 7:45 AM
noteve
noteve - avatar
+ 2
class Player: def __init__(self, name, lives): self.name = name self._lives = lives def hit(self): self._lives -= 1 if self._lives <= 0: print('Game Over') return self._lives p = Player("Cyberpunk77", 3) p.hit() p.hit() p.hit()
14th Feb 2022, 8:05 PM
Liria
Liria - avatar
+ 1
Yes it turned out 3 times, that's why it's wrong
11th Feb 2021, 9:56 AM
Ratna13
Ratna13 - avatar
+ 1
Rik Wittkopp thank you
11th Feb 2021, 9:56 AM
Ratna13
Ratna13 - avatar
+ 1
class Player: def __init__(self, name, lives): self.name = name self._lives = lives def hit(self): #your code goes here self._lives -=1 if self._lives <= 0: print("Game Over") return self._lives p = Player("Cyberpunk77", 3) p.hit() p.hit() p.hit()
22nd Aug 2022, 11:58 AM
Abdul Ghafar
Abdul Ghafar - avatar
0
I don't know what sololearn expects as results but nothing wrong with my code.
11th Feb 2021, 8:02 AM
Ratna13
Ratna13 - avatar
0
This is the case: 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.
11th Feb 2021, 8:04 AM
Ratna13
Ratna13 - avatar