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()
8 ответов
+ 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
+ 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.
+ 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()
+ 1
Yes it turned out 3 times, that's why it's wrong
+ 1
Rik Wittkopp thank you
+ 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()
0
I don't know what sololearn expects as results but nothing wrong with my code.
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.