Monster attack code not working (Text based RPG)

Can someone please tell me what is wrong with my code? The player can attack and make the monster take damage, but the monster won’t attack the player back for some reason. Please help me.

1 Like

I also would like to add up points when you beat the monster, and print the total points when you lose to the monster and say game over.

1 Like

Hi @DougieEarl!

The monster seems to be attacking and functioning as expected after playtesting. Are you still experiencing any issues?

1 Like

Yes, but not with that problem. It would be great if you could help.

Here are the problems I’ve experienced:

  1. Points are not added when the player defeats a monster
  2. The points aren’t tallied up when the monster defeats the player.
  3. The healing potions do not work.

Ok, I think I identified a problem, but I can’t seem to see any code related to points when the player defeats the monster.

if goblin.hp <= 0:
      print("You beat the monster!")

You seem to be calling break before calling print, so anything after break will not run.


i think I may have fixed that. There is one crucial bug that is more important that I cant figure out. Try playing the game and running from all monsters that spawn. Just explore. At certain points, the character is unable to go to a new location. I am unaware why this happens.

1 Like

By unable, is the code erroring out, or is some logic error occuring?

So, when you type in your input to the question, it tells you that you’re in the same location and didn’t move, and then if you type in your answer again, it just comes out with nothing. I’ll send a screenshot.

1 Like

1 Like

Think I found your problem: Some of your if statements use .lower, when you should actually be using .lower().

if location == "The Clearing":
      clearingdescision = input("In the clearing, you can see a path that leads to a swamp, or to your left, a path that leads to a mountain. Which path do you choose? (Swamp, Mountain)")
      if clearingdescision.lower == "swamp" or clearingdescision.lower == "s":
        location = "The Swamp"
        enterlocation(location, player)
      if clearingdescision.lower == "mountain" or clearingdescision.lower == "m":
        location = "The Mountains"
        enterlocation(location, player)

This is one of the affected ones, and I think there is some others too. Maybe try using CTRL+F to help locate them faster, but that should fix it.


Ok. i will try adding that.

1 Like

Another thing that I noticed is that he’s setting location to the new value within each function for handling a specific location, but he’s not returning the new value or setting the global location variable to the new value.

Because of that the global location variable remains the same. When the code calls enterlocation(location, player) , the code is still using the same old location value.

I think the best thing to do here is to use global location within each function.

Like this example:

def locationtext(location, player):
    global location  # Add the global function here
    if location == "The Forest":
        forestdescision = input("In the forest, you can see a clearing in the distance or a dark path off to the side. Which path do you take? (Clearing, Path)")
        if forestdescision.lower() == "clearing" or forestdescision.lower() == "c":
            location = "The Clearing"
            enterlocation(location, player)
        if forestdescision.lower() == "path" or forestdescision.lower() == "p":
            location = "The Dark Forest Path"
            enterlocation(location, player)
    # ...
1 Like

That worked! Thanks for the help!

If you have time, I have 1 other problem that I need to be solved.

  1. I don’t know how to make the player heal with the method I have coded in the function.
1 Like

If you have time, I have 1 other problem that I need to be solved.

  1. I don’t know how to make the player heal with the method I have coded in the function.

You could simply use player.heal(someAmount) inside of this statement.

elif battlechoice.lower() == "heal" or battlechoice.lower() == "h":

Ok thank you. One more problem has been made aware to me. Sometimes when you are exploring, the game will not travel to the next location. Do you know why this could be happening?

On line 248, you are using an equal to operator (==), rather than the assignment operator (=).

location == "The Forest" # Change this
location = "The Forest" # To This