Python Forum
Can't find error in code but Python throws exception - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Can't find error in code but Python throws exception (/thread-13295.html)



Can't find error in code but Python throws exception - Sandwich_masterX - Oct-09-2018

Hello-

I'm making a text-based RPG with Python, but when I run my script, Python throws an exception. I've narrowed down the place of error to the following:

str = 1
spd = 1
#First combat
print("Outside is a large spider. You engage it in combat.")
enemyhp = 1
enemyspeed = 0
print("The enemy's HP is ",enemyhp1,", and its speed is ",enemyspeed,".")
attack = input("Do you FLEE or FIGHT?")
#Fight...
if attack == "FIGHT":
	damagedealt = 2*str
	print("You slash it, dealing ",damagedealt," damage.")
	enemyhp1 = enemyhp1 - damagedealt
	print("The enemy's HP is now at ",enemyhp1,". It dies. You also gain 2 XP from killing it, putting you at 2.")
	xp = 2
#...or flight.
if attack == "FLEE" and enemyspeed < speed:
	print("You succesfully fled the enemy. You can only flee when your speed is greater than the enemy's, and it gives you half as much XP. You are now at 1 XP.")
	xp = 1
It's supposed to give a choice between fighting or fleeing. When one attacks it deals damage in accordance with the strength of the character, or if one flees and your speed is greater than the enemy's you escape it. There seem to be no errors, but if someone can look it over for me and point out one, I'd be very grateful.

Thanks!


RE: Can't find error in code but Python throws exception - micseydel - Oct-09-2018

What's stack trace for the exception? Can you hard-code the values to reproduce here? As-is, your code uses enemyhp1 before it's defined.


RE: Can't find error in code but Python throws exception - Sandwich_masterX - Oct-09-2018

Oh, the enemyhp1 was a typo. It was what was messing up the code. Thanks! I should've proof-read it more before posting.


RE: Can't find error in code but Python throws exception - ichabod801 - Oct-09-2018

You might want to check out the text adventures tutorial link in my signature. It's not clear, but it looks like you're using an if/then structure for your game. The dictionary structure discussed in the tutorial is a much easier way to go.