Thread: Black Jack

    #1
  1. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    There where the rabbits jump
    Posts
    556
    Rep Power
    11

    Black Jack


    Hello here is my problem:

    I have made parts of a 21(Black Jack) game.

    Code:
    from random import choice as Choice
    from Tkinter import *
    
    king = 10
    queen = 10  
    jack = 10
    ace = "ace"
    six = 6
    seven = 7
    eight = 8
    nine = 9
    ten = 10
    
    values = king, queen, jack, ace, six, seven, eight, nine, ten
    
    card_values = 0
    
    def hit():
        x = Choice(values)
        if x == "ace":
            if card_values >= 11:
                x = 1
            if card_values <= 10:
        card_values += x  ######
        
    def stay():
        print "Stay"
    
    root = Tk()
    root.title("Black Jack")
    root.minsize(200,200)
    
    count = StringVar()
    count.set(card_values)
    
    Button(root,text="Hit",command = hit).pack(side=RIGHT)
    Button(root,text="Stay",command = stay).pack(side=RIGHT)
    
    Label(root,text="Your hand:").pack(side=LEFT)
    Label(root,textvariable=count).pack(side=LEFT)
    
    root.mainloop()
    It is a GUI... as seen

    I get this error

    Code:
    Exception in Tkinter callback
    Traceback (most recent call last):
      File "E:\Python\lib\lib-tk\Tkinter.py", line 1345, in __call__
        return self.func(*args)
      File "H:\Programming\Python Scripts\BlackJack.py", line 25, in hit
        card_values += x  ######
    UnboundLocalError: local variable 'card_values' referenced before assignment
    And no it is not referenced before assigned:

    Code:
    card_values = 0
    After the assignments for card values.

    I have no clue where the error is.

    Please help me.
    And please no comments on the structure, I know it is crap and the cards could be different (this is a freaking development version)
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    394
    Rep Power
    51
    Hi!

    All I get is an IntentationError, so it should be
    Code:
        if card_values <= 10:
            card_values += x  ######
    Regards, mawe
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    There where the rabbits jump
    Posts
    556
    Rep Power
    11
    Why would it be that:

    That part is made to define Aces (1,11)

    Not to add the (x) to card_values
  6. #4
  7. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    As a rule I wouldn't create a variable in the global scope for use with-in a function unless the value is passed in and returned. This improved code readability & lets you reuse your functions. You should also move the parts of your program that do the running inside an if __name__ == '__main__' block. Again, this helps you write clear reusable programs/modules.

    Will get on with an example Black Jack program for you, but try to keep these points in mind .

    Hope this helps,

    Mark.
    programming language development: www.netytan.com Hula


IMN logo majestic logo spyfu logo threadwatch logo seochat tools logo