0

Hi
I have been working on a blackjack program and i have hit a brick wall as one of my Global assignments isn't working for some reason

import random
class Cards(object):
    def __init__(self):
        global cards
        global player  
        global comp
        global comp_tot
        global playert
        cards = {2:4,3:4,4:4,5:4,6:4,7:4,8:4,9:4,10:16,'ace':4}
        player = []
        playert = 0
        comp_tot=0
        comp = []
        print playert,'hi'
        print "The Game is Blackjack"
    def draw(self):
        f = cards.keys()
        a = random.choice(cards.keys())
        cards[a]-=1
        if cards[a] == 0:
            del cards[a]
            if len(cards)==0:
                print "out of cards"
                return False
        return a
    def First_turn(self):
        print comp_tot
        print cards
        print player
        print playert
        card1=self.draw()
        card2=self.draw()
        card2='ace'
        if card1 is not 'ace' and card2 is not 'ace':
            player.append(card1)
            player.append(card2)
            value=card1+card2
            print "your cards are:",card1,"and",card2
            print value
            playert+=value
        else:
            player.append(card1)
            player.append(card2)
            count =0
            if card1 =='ace':
                count = 'c1'
                if playert+11>21:
                    val = 1
                else:
                    val=11
            elif card2 == 'ace':
                count ='c2'
                if playert + 11 >21:
                    val = 1
                else:
                    val = 11
            
                

c = Cards()
c.First_turn()

every other global variable works exept playert. Any help on how to fix this would be appreciated. Oh also the error i am getting goes like this

Traceback (most recent call last):
  File "C:/Documents and Settings/Paul/My Documents/Python/projects/Blackjack/jack1.0.py", line 61, in <module>
    c.First_turn()
  File "C:/Documents and Settings/Paul/My Documents/Python/projects/Blackjack/jack1.0.py", line 30, in First_turn
    print playert
UnboundLocalError: local variable 'playert' referenced before assignment

oh and also sorry for all the added prints, they were for debugging purposes

3
Contributors
3
Replies
4
Views
9 Years
Discussion Span
Last Post by Paul Thompson
0

One advantage of the class structure is that you don't use globals but self.var_name.

import random
class Cards(object):
    def __init__(self):
        self.cards = {2:4,3:4,4:4,5:4,6:4,7:4,8:4,9:4,10:16,'ace':4}
        self.player = []
        self.playert = 0
        self.comp_tot=0
        self.comp = []
        print self.playert,'hi'
        print "The Game is Blackjack"
    def draw(self):
        f = self.cards.keys()
        a = random.choice(self.cards.keys())
        self.cards[a]-=1
        if self.cards[a] == 0:
            del self.cards[a]
            if len(self.cards)==0:
                print "out of cards"
                return False
        return a
    def First_turn(self):
        print self.comp_tot
        print self.cards
        print self.player
        print self.playert
        card1=self.draw()
        card2=self.draw()
        card2='ace'
        if card1 is not 'ace' and card2 is not 'ace':
            self.player.append(card1)
            self.player.append(card2)
            value=card1+card2
            print "your cards are:",card1,"and",card2
            print value
            playert+=value
        else:
            self.player.append(card1)
            self.player.append(card2)
            count =0
            if card1 =='ace':
                count = 'c1'
                if self.playert+11>21:
                    val = 1
                else:
                    val=11
            elif card2 == 'ace':
                count ='c2'
                if self.playert + 11 >21:
                    val = 1
                else:
                    val = 11
            
                

c = Cards()
c.First_turn()
1

Welcome to the world of self! Just a side note, the Python style guide recommends to start class names with upper case and function/method names with lower case. If you write larger programs, this really helps.

The Python style guide by GVR himself:
http://www.python.org/peps/pep-0008.html

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.