Yatzy,我如何引用我的播放器属性?

时间:2016-08-12 09:02:20

标签: python

我现在有一个很大的问题,我不能参考我的播放器属性,我的代码目前看起来像这样:

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
        #self.lista={"ones":0,"twos":0,"threes":0, "fours":0,"fives":0,"sixs":0,"abovesum":0,"bonus":0,"onepair":0,"twopair":0,"threepair":0,"fourpair":0,"smalladder":0,"bigladder":0,"house":0,"chance":0,"yatzy":0,"totalsum":0}

    def __repr__(self):
        return self.name

    def __str__(self):
        return self.name

    def countbonus(self):
        self.abovesum=self.ones+self.twos+self.threes+self.fours+self.fives+self.sixs
        if self.abovesum>=63:
            self.bonus+=50
            return self.abovesum, self.bonus
        else:
            return self.abovesum, self.bonus

    def counttotalsum(self):
        self.totalsum=self.abovesum+self.bonus+self.onepair+self.twopair+self.threepair+self.fourpair+self.smalladder+self.bigladder+self.house+self.chance+self.yatzy

    def add(self):
        moment=input("Where do you want to put your points?: ")  #ej klar
        points=input("How many points did you get?: ")
        self.lista

    def visa(self):
        for i in self.name:
            print(i)

def welcome():
    print("Welcome to the yahtzee game!")
    players = int(input("How many players: "))
    rounds=0
    spelarlista=[]
    spelarlista.append("name")
    while not players==rounds:
        player=input("What is your name?: ")
        rounds=rounds+1
        spelarlista.append(Player(player))
    table_data = [spelarlista,
        ['Ettor',spelarlista[0].ones],
        ['Tvåor'],
        ['Treor'],
        ['Fyror'],
        ['femmor'],
        ['sexor']]
    table = AsciiTable(table_data)
    table.inner_row_border = True
    table.table_data[0][0] += '\n'
    print(table.table)
welcome()

我目前正在收到错误消息“Str” - 对象没有属性“ones”。我知道线路spelarlista [0] .ones不起作用,但是让我说我​​和玩家“詹姆斯”和“安德斯”一起玩这个节目,我希望我的yatzy用玩家当前得分打印出桌子,如何我是指那个,让我说我先输入“詹姆斯”,我写什么来得到他的观点?

提前致谢!

1 个答案:

答案 0 :(得分:3)

spelarlista.append("name")表示列表中的第一项是字符串。然后,您尝试访问spelarlista[0].ones

如错误消息所示,字符串对象没有属性ones