Python Forum
Var Not defined - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Var Not defined (/thread-5019.html)



Var Not defined - RainbowWolfy - Sep-14-2017

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()


RE: Var Not defined - ichabod801 - Sep-14-2017

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.


RE: Var Not defined - OmarBrikaa - Sep-14-2017

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()