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