0

Preservation challenge (intermediate Python - 23.2)

I feel like I'm close, but I can't seem to get the lives to decrease with each method call. I've also tried self._lives -= 1 (my initial idea), but it says 'invalid syntax'. After that I considered using an operator overload definition to define '-=', but after a few attempts I got confused as to how to input that, and usually when I start doing stuff like that it turns out the solution was much simpler than what I was attempting to pull off aha. Gotten the same output, 3 '2's with a while loop, but yield caused some <generator> error so I abandoned that. And I don't think it should require a loop? Anyways, here's my code, 'tips/directions' > 'handing me the answer'!: class Player: def __init__(self, name, lives): self.name = name self._lives = lives def hit(self): #your code goes here if self._lives==0: print("Game Over") else: return self._lives-1 p = Player("Cyberpunk77", 3) print(p.hit()) print(p.hit()) print(p.hit())

19th Mar 2021, 6:17 PM
Dom
5 ответов
+ 2
Okay, that gave '2; 1; game over', but didn't actually solve challenge/produce desired output, however gave me insight for the cleaner working code, which included a nested if statement to restrict 'returns': Edit: ***prev solution note*** Solution above (3 while loops) also worked if the third while loop was an 'if' statement (produces one output instead of repeated) AND if 'return "Game Over" was changed to 'print("Game Over")' (produces output instead of simply returning "Game Over") class Player: def __init__(self, name, lives): self.name = name self._lives = lives def hit(self): #your code goes here while self._lives>0: self._lives-=1 if self._lives > 0: return self._lives if self._lives==0: print("Game Over") p = Player("Cyberpunk77", 3) p.hit() p.hit() p.hit()
23rd Mar 2021, 6:44 PM
Dom
0
Split into separate ones.. self._lives -= 1 return self._lives edit : class Player: def __init__(self, name, lives): self.name = name self._lives = lives def hit(self): #your code goes here while self._lives>0: self._lives-=1 return self._lives if self._lives<=0: return ("Game Over") p = Player("Cyberpunk77", 3) print(p.hit()) print(p.hit()) print(p.hit())
19th Mar 2021, 7:12 PM
Jayakrishna 🇮🇳
0
***Almost Solution!*** (edited solution below) (big thanks to Jayakrishna🇮🇳 for moving this forward, got "2", "1", "0" from that code but still no 'game over', whether I nested the if statement or made it come prior) Breaking this method into separate loops and deleting the 'return' for lives = 1 did the trick, guess it was returning 0 and ending the loop before evaluating the 'If' condition for a game over. Deleted line highlighted with ***" "*** Edit: thought this worked at first class Player: def __init__(self, name, lives): self.name = name self._lives = lives def hit(self): #your code goes here while self._lives >= 2: self._lives -= 1 return self._lives while self._lives == 1: self._lives -=1 ***return self._lives*** while self._lives == 0: return "Game Over" p = Player("Cyberpunk77", 3) print(p.hit()) print(p.hit()) print(p.hit())
23rd Mar 2021, 6:29 PM
Dom
0
My solution is; also is possible to use one line if statements too 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") else: return self._lives
26th Dec 2021, 11:00 PM
Furkan
- 1
While loop: class Player: def __init__(self, name, lives): self.name = name self._lives = lives def hit(self): #your code goes here while self._lives>0: return self._lives-=1 if self._lives==0: print("Game Over") p = Player("Cyberpunk77", 3) print(p.hit()) print(p.hit()) print(p.hit()) '-=1' outputs 'invalid syntax', '-1' outputs '2' three times Even tested by setting Cyberpunk77's lives to 0 and got a game over output, feel like I'm so close! Feel like I'm simply indenting wrong or should be using yield or something
19th Mar 2021, 6:18 PM
Dom