Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

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.

Become a Member to join the conversation.