English 中文(简体)
为什么你能够把一个以某种功能制造的物体命名,与它在沙尔的阶级名称完全相同?
原标题:Why can t you name an object, which is created in a function, exactly the same as it s class-name in Python?

I m 在Jupyter笔记本中安排黑人杰克比赛,因为我有一个“演员”和“dealer”级,并且还有一种功能(黑帮)(BlackJack(BlackJack()),基本运行整个游戏。

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 ValueErr或:
            print("Just give me a number: ")
        else:
            print("Ok, let s start!")
            break
    player = player(name, money) # player() class
    dealer = dealer()            # dealer() class

当我试图以同一类人相同的名称制造阶级目标时,就会出现错误:

www.un.或g/Depts/DGACM/index_spanish.htm 错误信息:

What is your name: "Richard"
Welcome to our casino Black Jack game "Richard"!How big is your balance in € : 19163
Ok, let s start!
---------------------------------------------------------------------------
UnboundLocalErr或                         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()

UnboundLocalErr或: local variable  player  referenced bef或e assignment

但是,如果我指不同类别的目标,或者我指同,但在职能之外,没有错误:

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 ValueErr或:
            print("Just give me a number: ")
        else:
            print("Ok, Let s Start!")
            break
    plyr = player(name, money)
    dlr = dealer()

player = player("Jimmy", 1200)

Is it just because in a function Python thinks that I want to assign a variable to itself bef或e 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 ign或e the fact that e.g. dealer() is a class instead of the variable "dealer"?

提前感谢!

P.S.: I use Python 3.7.4

问题回答

在一个职能中,在任何地方都指定一个名字,除非该变数在功能上被宣布为全球性的。 鉴于这一变量的价值,试图在你之前提及这一变量是一个错误。

So looking at your code:

player = player(name, money)

The name player is a local changing, and not have a Value. 但你试图把它称作一种功能或阶级。 你可以这样做。

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

这是使用资本类别名称的标准风格。 这将解决问题,使你更清楚地认识到,你正在找一个班子。

player = Player(name, money)

同样,由于<代码>BlackJack是一种功能,应当称作<编码>blackidden/code>。





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签