伙计们不知道为什么当我尝试运行startGame函数时,试图为已经定义的对象获取名称错误,尝试创建基于文本的黑 jack 游戏,但是却为已经定义的列表获取错误(人员和发牌人)(实际上,该函数会随机产生两个发给交易商和个人的卡片,并分别添加这两个值)
输入
def rand():
king=10
jack=10
queen=10
one=2
two=3
three=4
four=5
five=6
six=7
seven=8
eight=9
nine=10
import random
return random.choice([king,queen,jack,one,two,three,four,five,six,seven,eight,nine,])
class black():
person=[]
dealer=[]
def __init__(self,rando):
self.rando=rando
def StartingGame(self):
import math
person.append(rand())
person.append(rand())
dealer.append(rand())
dealer.append(rand())
return math.fsum(person),math.fsum(dealer)
输出tt=black(rand())
tt.StartingGame()
NameError Traceback (most recent call last)
<ipython-input-4-adf1a6aa555c> in <module>
----> 1 tt.StartingGame()
<ipython-input-1-ad7bd57d22e4> in StartingGame(self)
26 import math
27
---> 28 person.append(rand())
29 person.append(rand())
30 dealer.append(rand())
NameError: name 'person' is not defined
最佳答案
您应该使用self.person
,因为LEGO规则中不包括class
范围。
关于python - 对于已经定义的列表对象,出现名称错误。应该提供任何可能的帮助,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62733677/