+ 1

Intermediate Python- 25.2 Practice help - “Staying alive”

Please help, thanks! We are improving our game and need to add an isAlive property, which returns True if the lives count is greater than 0. Complete the code by adding the isAlive property.

18th Jul 2021, 4:04 PM
Zach Z
9 Answers
+ 6
You're missing to add isAlive property. @property def isAlive(self): if self._lives > 0: return True
30th Jul 2021, 5:51 PM
Simba
Simba - avatar
+ 3
Simba and JaScript have solutions to your issue. The only change I would make is instead of return True or return False just return self._lives > 0 as returning this will give the correct result without the redundancy, requires less code, and is more Pythonic IMO.
30th Jul 2021, 8:02 PM
ChaoticDawg
ChaoticDawg - avatar
+ 2
8th Aug 2021, 8:59 PM
Zach Z
+ 1
Zach Z Where is your attempts?
18th Jul 2021, 6:04 PM
A͢J
A͢J - avatar
+ 1
✩✮★✮✩ , Simba , ChaoticDawg , A͢J , JaScript You all have been super helpful, same as the other question i just tagged, can one of you help here? Thanks!
30th Jul 2021, 5:30 PM
Zach Z
30th Jul 2021, 6:52 PM
JaScript
JaScript - avatar
+ 1
class Player: def __init__(self, name, lives): self.name = name self._lives = lives def hit(self): self._lives -= 1 #your code goes here @property def isAlive(self): if self._lives > 0: return True p = Player("Cyberpunk77", int(input())) i = 1 while True: p.hit() print("Hit # " + str(i)) i += 1 if not p.isAlive: print("Game Over") break
14th Feb 2022, 10:11 PM
Liria
Liria - avatar
0
Code: class Player: def __init__(self, name, lives): self.name = name self._lives = lives def hit(self): self._lives -= 1 p = Player("Cyberpunk77", int(input())) i = 1 while True: p.hit() print("Hit # " + str(i)) i += 1 if not p.isAlive: print("Game Over") break
18th Jul 2021, 4:04 PM
Zach Z
0
Code is there
20th Jul 2021, 7:22 PM
Zach Z