Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Can't find error in code but Python throws exception
#1
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!
Quote
#2
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 :)
Quote
#3
Oh, the enemyhp1 was a typo. It was what was messing up the code. Thanks! I should've proof-read it more before posting.
Quote
#4
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 - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Error in python code 'import callbot' Lou_Reed 3 188 Jan-07-2020, 09:33 PM
Last Post: Clunk_Head
  Continuing code after exception palladium 1 160 Dec-14-2019, 02:43 PM
Last Post: ichabod801
  i am getting error while writing code for exception logging rkgupta51179 1 189 Nov-03-2019, 05:12 AM
Last Post: buran
  How to fix 'uncaught exception of type NSException' in Python MonsterPython 0 296 Jul-09-2019, 06:52 AM
Last Post: MonsterPython
  problem using custom exception handling in python srm 3 379 Jul-03-2019, 09:10 PM
Last Post: ichabod801
  Can you find the problem in my code? ivinjjunior 3 393 Apr-20-2019, 12:37 AM
Last Post: ivinjjunior
  How to fix error code 2 in python, “directory not found”? dav3javu 1 534 Apr-03-2019, 04:55 PM
Last Post: Larz60+
  Indentation error in Python code ErnestTBass 5 656 Feb-28-2019, 04:26 PM
Last Post: samsonite
  How to ignore - ERROR: The system was unable to find the specified registry key or va asheru93 9 1,377 Feb-04-2019, 06:35 AM
Last Post: asheru93
  During handling of the above exception, another exception occurred Skaperen 7 9,098 Dec-21-2018, 10:58 AM
Last Post: Gribouillis

Forum Jump:


Users browsing this thread: 1 Guest(s)