So, I'm still a beginner at python and OOP in general. But I had a question about the order of operations.
Basically what I have is a module that contains a class that will reset all self variables for the object and give the object a card. The script I'm using to test this with looks like this:
import testmod
p1 = testmod.playerClass()
p1.resetAll()
p1.giveCard(1)
And... it doesn't work. It tells me an internal variable is being accessed before it's defined. What I don't understand is why it works if I enter it in line by line in the shell. Are these two functions running at the same time? Is one running before the other?
The module code (excuse the crudity):
"""-------------------------------------------------------
testmod
-------------------------------------------------------"""
import random
class playerClass:
"""-------------------------------------------------------
Reset all self vars
-------------------------------------------------------"""
def resetAll(self):
self.Tcard = ""
self.Sum = 0
self.Ace = 0
""" ------------------------------------------------------
Give player cards
------------------------------------------------------"""
def giveCard(self, Number): #give player N card(s)
while(Number > 0):
self.Tcard = random.randint(2,14)
if(self.Tcard > 11): #all number cards
self.Sum+=self.Tcard
if(self.Tcard <= 11 & self.Tcard != 14): #all faces but ace
if(self.Tcard == 11):
self.Tcard = "J"
if(self.Tcard == 12):
self.Tcard == "Q"
if(self.Tcard == 13):
self.Tcard == "K"
self.Sum+=10
if(self.Tcard == 14): #on ace
self.Tcard == "A"
self.Sum += 11
self.Ace += 1
Number-=1
print "REPORT------------------------------"
print " %s given" % self.Tcard
print " sum %s" % self.Sum
print " ace %s" % self.Ace
And here's the error:
Traceback (most recent call last):
File "C:\Documents and Settings\Dr.Wafflestomper\Desktop\Python\bjtest.py", line 8, in <module>
p1.giveCard(1)
File "C:\Python26\getcard.py", line 22, in giveCard
if(self.Tcard > 11): #all number cards
UnboundLocalError: local variable 'Tcard' referenced before assignment
Any help is appreciated.