Skip to content
Advertisement

How do I stop characters from attacking after they die?

I’m working on a python text-based adventure and I can’t figure out how to stop a character from doing damage once they die. The program works fine when I run it, but even after a character dies, they can still attack the other characters. I’ve looked on google but I can’t find any relevant info

Here’s a snippet of my code:

JavaScript

Advertisement

Answer

The problem is this if statement

JavaScript

It is run before the health check is run, and even if the health check was run, it still continues on to the above if statement.

You should edit your sword function to be like this:

JavaScript

If the warrior’s health is below zero, the function returns and ends, and prints that the warrior is already dead.

Another way to handle it without returning to change this if statement:

JavaScript

To an elif statement:

JavaScript

So that it only gets run if the warrior has health left, or else it is skipped. You still have to put the health check above that statement, else it wont work.

Advertisement