Join us and get access to thousands of tutorials and a community of expert Pythonistas.
This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.
Implement Healing
00:00
The second conditional is going to be to give the player a chance to heal and regain 30 health when they choose to do so. That’ll be another action that I’m going to chain on here by saying elif if the action is not an "A", but instead it’s going to be uppercase "H", then I want the player to regain 30 health, but it shouldn’t go over a maximum of 100. This is not a random value.
00:28
This is always going to be 30. So I could define it in here, but then it would define this variable every time that the user presses H, which is not really necessary because it’s not changing.
00:38
This is why I’m going to put it up here with the first initialization. I’m going to say that heal means 30. Every time the player heals, it’s going to be 30 points, and we don’t need to redefine that variable every time the action gets called.
00:52
Similar to before, I can just use += to add it to the player_health.
00:59
+= heal. Those are at the 30. The problem here is going to be, this can still exceed the maximum of 100 for now. Let’s give it a go actually. If I run this, you can see that if I now press H for heal, well, I’ve also got to print out what the player_health is to see anything.
01:23
player_health. Run it again.
01:28
Okay, I want to heal. I selected h for heal. It went into the right conditional, but my player’s health went above 100. We don’t want that to happen. If the player_health after healing
01:44
is bigger than 100, then we’re just going to reset it to 100. I’m going to say player_health = 100, so that means that it’s going to heal anything up to 100, and if it goes over 100, this small conditional here triggers the intended code block and resets the player_health to 100.
02:05
So now, when we start off and heal as the first step, it should mean that the player is still at 100 health. All right, looking good.
02:16 So we tackled the second conditional, as well. It’s time to look at the third one.
Martin Breuss RP Team on Sept. 18, 2026
Hi Mark,
Your approach can work, but only assuming you meant action == "H" rather than player_health == "H"? And also that this follows a preceding if statement (you’re currently starting with elif.
Note that your code allows the player to heal only when their health is 70 or lower.
Since the healing amount is 30, a health value of 70 becomes exactly 100, and lower values won’t exceed the maximum. However, a player with health from 71 through 99 won’t heal at all because the condition player_health <= 70 is false. The solution allows healing at any value below 100 and then caps the result at 100.
So your version is valid if that restricted healing behavior is intentional. If the player should be able to heal whenever they’re below full health, the health cap approach is more flexible.
Become a Member to join the conversation.
Mark Olivier on Sept. 15, 2026
Hi,
My logic for player_health was slightly different to the solution. When I tested, it seemed to work OK. Do you see any issues with my version.