2016-08-13 2 views
-4

내 문제는 현재 내 yahtzee에 대한 코드가 있습니다. (모든 코드를 게시하지는 않지만 중요한 내용은 다음과 같을 것입니다 :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() 

지금 문제는 내가, 당신은 내가 [player.ones for player in spelarlista]가 나는 것을 볼 수 있습니다 당신이 내 환영 방법을 살펴 경우 모든 self.ones, self.twos 등 대신에 사전을 추가 할 것입니다 이 점이 플레이어 포인트를 할당하는 데 필요합니다. 어떻게하면 사전에 대한이 작업을 해결할 수 있습니까? 궁금한 점이 있다면 다음 사전에 사전에 추가해야합니다.

미리 감사드립니다!

답변

1
dict_names = ['ones', 'twos', ..., 'totalsum'] 
self.dict = {name:0 for name in dict_names} 

하고 player.dict['fours'] 대신 player.fours 경우

+0

덕분에 도움을 많이 사용하는 것 액세스 할 수! 대단히 감사합니다! –