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
  python/winrt Bluetooth.GenericAttributProfile CreateAsync(GUID) method throws Not Imp pbvinoth 0 117 Jul-08-2020, 04:27 AM
Last Post: pbvinoth
  Error in Python Code ErnestTBass 4 165 Jun-04-2020, 05:28 PM
Last Post: buran
  Python Code error Riteshfrancis 2 198 May-21-2020, 06:37 AM
Last Post: Riteshfrancis
  Cant find root cause of thread.lock error burlyboys 0 142 May-18-2020, 12:51 PM
Last Post: burlyboys
  Exception handling in regex using python ShruthiLS 1 213 May-04-2020, 08:12 AM
Last Post: anbu23
  Find the realpath for a symlinked .pth file in itself with python code. hongyi 7 324 Apr-27-2020, 05:30 AM
Last Post: Larz60+
  Block of code, scope of variables and surprising exception arbiel 8 462 Apr-06-2020, 07:57 PM
Last Post: arbiel
  Simple python code error cls0724 5 460 Mar-12-2020, 07:45 PM
Last Post: stullis
  Exception: Returned Type Mismatch Error devansing 1 373 Mar-06-2020, 07:26 PM
Last Post: ndc85430
  Error in python code 'import callbot' Lou_Reed 3 371 Jan-07-2020, 09:33 PM
Last Post: Clunk_Head

Forum Jump:

Users browsing this thread: 1 Guest(s)