Yahtzee,使用字典而不是属性列表

时间:2016-08-13 13:18:58

标签: python

所以我的问题是目前我的代码是yahtzee(我不会发布所有代码,但重要的东西看起来像这样(并且它有效):

from terminaltables import AsciiTable

class Player:
    def __init__(self,name):
        self.name=name
        self.ones=0
        self.twos=0
        self.threes=0
        self.fours=0
        self.fives=0
        self.sixs=0
        self.abovesum=0
        self.bonus=0
        self.onepair=0
        self.twopair=0
        self.threepair=0
        self.fourpair=0
        self.smalladder=0
        self.bigladder=0
        self.house=0
        self.chance=0
        self.yatzy=0
        self.totalsum=0

def welcome():
    global spelarlista
    spelarlista=[]
    print("Welcome to the yahtzee game!")
    players = int(input("How many players: "))
    rounds=0
    while not players==rounds:
        player=input("What is your name?: ")
        rounds=rounds+1
        spelarlista.append(Player(player))
    table_data = [["Name"] + spelarlista,
        ['Ones'] + [player.ones for player in spelarlista],
        ['Twos'] + [player.twos for player in spelarlista],
        ['Threes'] + [player.threes for player in spelarlista],
        ['Fours'] + [player.fours for player in spelarlista],
        ['Fives'] + [player.fives for player in spelarlista],
        ['Sixs'] + [player.sixs for player in spelarlista]]
    table = AsciiTable(table_data)
    table.inner_row_border = True
    print(table.table)
    spelarlista[0].add()
welcome()

现在的问题是我想添加一个字典而不是所有那些self.ones,self.twos等。如果你看看我的欢迎方法,你可以看到我有[player.ones for player in spelarlista]和我需要这个来分配玩家积分,我该如何解决这个问题呢?如果你很好奇的话,这个词典是我下一个方法的必要条件!

提前致谢!

0 个答案:

没有答案