Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Var Not defined
#1
So I'm pretty new to Python, although I got alot of experience in other languages.
I have a problem where my Attack variable is not being defined

Error:
if Attack == "1":
There's my code:

class Battle1:
def StartBattle():



print("")
print("YOU: How Dare You Insult My Country! You Shall Die!")
print("...................................................")
print("...................................................")
print("HP: " + str(HP) + " ENEMY HP: " + str(EHP))
print("...................................................")
print("...................................................")
print("Slash (20-30 DMG) [1]")
print("FireSpell (15-40 DMG) [2]")
print("Swift Arrow (10-60 DMG) [3]")
Attack = input("What Attack Do You Use? [1,2,3]")
if Attack == "1":
print("CHECK")
AttackDmg = randint(20,30)
EHP = EHP - AttackDmg
print("BATTLE: You Slashed Him With Your Mighty Sword, You Dealt: " + str(AttackDmg)+ " Damage!")
print("BATTLE: Your Enemy Has: " + str(EHP) + "Health")
print("")
print("GATEKEEPER: You Will Not Live Any Longer.")
EDMG = randint(10,25)
HP = HP - EDMG
print("BATTLE: GateKeeper Dealt: " + str(EDMG))
StartBattle()
Reply
#2
It would be helpful if you could post the code in python tags, see the BBCode link in my signature below for instructions. Then we could see the indentation, which is important.

Looking at your code, I don't see how you would get that error. Attack is defined on the line before you used it. I would expect it for HP and EHP, which you use before defining them. A variable must be on the left side of an assignment (=) before you can use it on the right side of an assignment, in a comparison (==), or pass it to a function.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Hello RainbowWolfy,
You can either use:
Attack = raw_input("What Attack Do You Use? [1,2,3]")
Or
print("What Attack Do You Use? [1,2,3]")
Attack = input()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python library not defined in user defined function johnEmScott 2 3,918 May-30-2020, 04:14 AM
Last Post: DT2000

Forum Jump:

User Panel Messages

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