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.

File "C:\Python26\getcard.py", line 22, in giveCard

It appears the error is in getcard.py, which you did not post, and not in testmod.py which you did post. The code you posted worked fine for me. BTW the following will only work for self.Tcard == 11, not if it equals 12 or 13.

if(self.Tcard <= 11 & self.Tcard != 14): 
              =================
                if(self.Tcard == 11):
                    self.Tcard = "J"
                if(self.Tcard == 12):
                    self.Tcard == "Q"
                if(self.Tcard == 13):
                    self.Tcard == "K"
                self.Sum+=10 
#
##----- a dictionary would work better than multiple if statements
card_dict = [ 11:"J", 12: "Q", 13:"K"}
if self.Tcard in card_dict:
    self.Tcard = card_dict[self.Tcard]

Finally, take a look at the Python Style Guide when you have time. It is easier to debug other people's code when we all use the same conventions http://www.python.org/dev/peps/pep-0008/

It appears the error is in getcard.py, which you did not post, and not in testmod.py which you did post. The code you posted worked fine for me. BTW the following will only work for self.Tcard == 11, not if it equals 12 or 13.

if(self.Tcard <= 11 & self.Tcard != 14): 
              =================
                if(self.Tcard == 11):
                    self.Tcard = "J"
                if(self.Tcard == 12):
                    self.Tcard == "Q"
                if(self.Tcard == 13):
                    self.Tcard == "K"
                self.Sum+=10 
#
##----- a dictionary would work better than multiple if statements
card_dict = [ 11:"J", 12: "Q", 13:"K"}
if self.Tcard in card_dict:
    self.Tcard = card_dict[self.Tcard]

Finally, take a look at the Python Style Guide when you have time. It is easier to debug other people's code when we all use the same conventions http://www.python.org/dev/peps/pep-0008/

You're right. I think I confused myself when I was naming these modules =S. Not entirely sure what was going on, but I got it working on my side. Thanks for the help man :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.