0

I'm programing a Black Jack game in a Jupyter notebook and for that I have a "player" and a "dealer" class and also a function (BlackJack()) which basically runs the entire game.

def BlackJack():   
    name = input("What is your name: ")
    while True:
        try:            
            money = int(input(f"Welcome to our casino Black Jack game {name}!How big is your balance in € : "))
        except ValueError:
            print("Just give me a number: ")
        else:
            print("Ok, let's start!")
            break
    player = player(name, money) # player() class
    dealer = dealer()            # dealer() class

An error occurs when I try to create class-objects with the same name as the class itself:

Error message:

What is your name: "Richard"
Welcome to our casino Black Jack game "Richard"!How big is your balance in € : 19163
Ok, let's start!
---------------------------------------------------------------------------
UnboundLocalError                         Traceback (most recent call last)
<ipython-input-55-3c92f609e237> in <module>
----> 1 BlackJack()

<ipython-input-54-57fc63786581> in BlackJack()
      9             print("Ok, let's start!")
     10             break
---> 11     player = player(name, money)
     12     dealer = dealer()

UnboundLocalError: local variable 'player' referenced before assignment

But if I name the class-objects different or I name them the same, but outside a function there is no error:

def BlackJack():   
    name = input("What is your name: ")
    while True:
        try:            
            money = int(input(f"Welcome to our casino Black Jack game {name}!How big is your balance in € : "))
        except ValueError:
            print("Just give me a number: ")
        else:
            print("Ok, Let's Start!")
            break
    plyr = player(name, money)
    dlr = dealer()

or

player = player("Jimmy", 1200)

Is it just because in a function Python thinks that I want to assign a variable to itself before I've even assigned the variable (dealer = dealer()), even if they are not actually the same, because one is a variable and the other is a class? So does Python in this case just ignore the fact that e.g. dealer() is a class instead of the variable "dealer"?

Thanks in advance!

P.S.: I use Python 3.7.4

s.dhruvi
  • 43
  • 11

1 Answers1

4

Assigning to a name anywhere in a function makes it a local variable, unless the variable has been declared global in the function. Attempting to refer to this variable before you've given it a value is an error.

So looking at your code:

player = player(name, money)

The name player is a local variable, and does not yet have a value. But you are attempting to call it like a function or class. You can't do that.

If you use a different name on the left side, as you discovered, player refers to the class you defined and it works.

This is (one reason) it is standard Python style to use capitalized class names. This will solve the problem and make it clearer that you are instantiating a class.

player = Player(name, money)

Similarly, because BlackJack is a function, it should be named blackjack.

kindall
  • 178,883
  • 35
  • 278
  • 309
  • Ok, I see. So by the LEGB-Rule (Local, Enclosing function local, Global, Built-in) it thinks that "player(name, money)" has something to do with the local variable "player" and not with the class "player()" itself, because I "called" the variable "player" , in the statement "**player** = player(name, money)"? – Mihail Stefanov Mar 17 '20 at 17:35
  • @MihailStefanov no, because you used an assignment statement using that name, the *compiler* marks it as local. But even with the scope issue in this particular case, you shouldn't overwrite the name that the class is assigned to. – juanpa.arrivillaga Mar 17 '20 at 17:59
  • Yes, `player()` is taken to refer to the local variable `player` which you haven't defined yet. In Python, the names of classes (and functions, for that matter) are not any way special; a class is just one kind of value that a variable can refer to. – kindall Mar 17 '20 at 21:35