我试图用 Python 编写一个国际象棋“瑞士”锦标赛经理。我有两个班,一个比赛班和一个球员班。我还有两个字典,name_object 将字符串名称绑定到对象,name_score 将 Player 对象的名称绑定到 Player 对象的分数(1.0、2.5 等)。
变量示例
name_object = {'Jim' : Object, 'Jack' : Object}
name_score = {'Jim': 2.5, 'Jack' : 1.0}
我的课程
class Player:
def __init__(self, name, gender, rating, score):
self.name = name
self.gender = gender
self.rating = rating
self.score = score
class Match:
def __init__(self, white, black):
self.white = white
self.black = black
def Result(self, res):
global name_object, name_score
complete = False
if res == 1:
if complete == False:
name_object[self.white].score += 1
name_score[self.white] += 1
complete = True
elif res == 0:
if complete == False:
name_object[self.black].score += 1
name_score[self.black].score += 1
complete = True
elif res == 'd':
if complete == False:
name_object[self.white].score += .5
name_score[self.white] += .5
name_object[self.black].score += .5
name_score[self.black].score += .5
但是当我尝试用两个玩家名字“Jim”和“Jack”创建一个对象时,
Match1 = Match(name_object['Jim'], name_object['Jack'])
然后尝试以白胜调用Result方法,
Match1.Result(1)
我得到这个错误,
line 43, in Result
name_object[self.white].score += 1
KeyError: <__main__.Player object at 0x02AB6DF0>
非常感谢所有帮助