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

Handle Invalid Input

00:00 And as if I wanted to make a point that taking breaks is a good idea when coding, I came back from stretching to realize that there’s still two problems in this code.

00:12 The first one is that I just copied this call to print() that tells the user about the health of the player and the monster instead of moving it.

00:22 So, it’s been printing out twice all the time, and I didn’t even notice that. Second, there’s also, like, the action still printed out, which is something that we don’t need anymore.

00:33 So, first little fix is to just remove these two calls to print() and clean out the text input a little bit more. But there’s also a bigger issue in this code. I’m going to run it, and then you might have noticed that as well.

00:51 So far, attacking works, healing works, and running works too. I’m not going to do it right now, but the problem is that if I enter anything else than the selected options, the game doesn’t quite work as expected.

01:06 What happens is the monster still gets a chance to attack me, but I can’t do anything. I don’t heal, I don’t attack, and I don’t really run away either. So I’m kind of stuck in this position where I keep getting beat up by a monster without being able to do anything about it. Help!

01:25 And there is no way to get out of this. So there’s a little bug in here, right? And it’s a bit subtle because the game keeps going. So, you might not even notice it right away, like I didn’t notice it up to taking a break.

01:40 What we forgot to add here is to handle other inputs than the expected ones, and we did set up the structure for that. So I have this ifelifelif structure, and I think we even talked about then adding an else clause later on that would just catch the rest, anything that isn’t unexpected output, but we didn’t implement it. Let’s go ahead and do this now.

02:06 After the second elif, I’m going to add an else clause, and that should just catch any other user input and print out a message, tell them something like

02:20 "Invalid action. Please choose A, H, or R." So I’m telling them this is not going to work by saying there’s an invalid action and then also giving them a way out. Instead, they should press A, H, or R.

02:36 I also need to prevent the monster from attacking because, well, maybe you want a hard version of the game where if the user inputs an invalid character, the monster still gets to attack. But in this case, I want to make it a little more user-friendly, I guess, or just easy mode.

02:56 I’m going to use the other looping keyword, continue, underneath here, which means that if this else clause catches, the continue statement is going to bring us back to the beginning of the while loop, which means the user has another chance to input a character.

03:13 So in that case, if the user inputs anything except A, H, or R, they’re going to get a useful message and get redirected back to the beginning of the loop, where they have another chance. Let’s try that out.

03:28 Okay, ddd. So, it now tells me Invalid action. Please choose A, H, or R. And both my health and the monster health are still at the beginning value.

03:39 So, that’s a good addition that makes the game a little more relaxing, I would say. With this, I feel relaxed enough to take the next step and look at the stretch goals.

Become a Member to join the conversation.