Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Can't find error in code but Python throws exception

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?")
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.

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.
Feel like you're not getting the answers you want? Checkout the help/rules for things like what to include/not include in a post, how to use code tags, how to ask smart questions, and more.

Pro-tip - there's an inverse correlation between the number of lines of code posted and my enthusiasm for helping with a question :)
Oh, the enemyhp1 was a typo. It was what was messing up the code. Thanks! I should've proof-read it more before posting.
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.
Craig "Ichabod" O'Brien -
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures


Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Simple python code error cls0724 5 288 Mar-12-2020, 07:45 PM
Last Post: stullis
  Exception: Returned Type Mismatch Error devansing 1 114 Mar-06-2020, 07:26 PM
Last Post: ndc85430
  Error in python code 'import callbot' Lou_Reed 3 211 Jan-07-2020, 09:33 PM
Last Post: Clunk_Head
  Continuing code after exception palladium 1 189 Dec-14-2019, 02:43 PM
Last Post: ichabod801
  i am getting error while writing code for exception logging rkgupta51179 1 208 Nov-03-2019, 05:12 AM
Last Post: buran
  How to fix 'uncaught exception of type NSException' in Python MonsterPython 0 318 Jul-09-2019, 06:52 AM
Last Post: MonsterPython
  problem using custom exception handling in python srm 3 412 Jul-03-2019, 09:10 PM
Last Post: ichabod801
  Can you find the problem in my code? ivinjjunior 3 405 Apr-20-2019, 12:37 AM
Last Post: ivinjjunior
  How to fix error code 2 in python, “directory not found”? dav3javu 1 605 Apr-03-2019, 04:55 PM
Last Post: Larz60+
  Indentation error in Python code ErnestTBass 5 717 Feb-28-2019, 04:26 PM
Last Post: samsonite

Forum Jump:

Users browsing this thread: 1 Guest(s)