Python Forum
Can't find error in code but Python throws exception
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!
Reply
#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.
Reply
#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.
Reply
#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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question in this code, I input Key_word, it can not find although all data was exact Help me! duchien04x4 3 971 Aug-31-2023, 05:36 PM
Last Post: deanhystad
  Code error from Fundamentals of Python Programming van Richard L. Halterman Heidi 12 1,600 Jul-25-2023, 10:32 PM
Last Post: Skaperen
  Syntax error while executing the Python code in Linux DivAsh 8 1,450 Jul-19-2023, 06:27 PM
Last Post: Lahearle
  Compiles Python code with no error but giving out no output - what's wrong with it? pythonflea 6 1,468 Mar-27-2023, 07:38 AM
Last Post: buran
  Error when using FIND WJSwan 2 855 Jan-27-2023, 05:49 AM
Last Post: deanhystad
Question log.exception() without arguments in old Python versions? cthart 5 1,116 Nov-19-2022, 07:09 PM
Last Post: Gribouillis
  how to write exception error into logger mg24 3 947 Nov-15-2022, 04:20 PM
Last Post: insharazzak
  Error in if-then-else python code Led_Zeppelin 6 2,283 Jul-27-2022, 07:53 PM
Last Post: deanhystad
  Error in find pearson correlation function erneelgupta 1 1,809 Mar-01-2022, 03:41 PM
Last Post: stevendaprano
  pymysql: insert query throws error wardancer84 12 4,417 Jan-28-2022, 06:48 AM
Last Post: wardancer84

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020