来自未调用的对象的属性错误

Attribute error from an object that isn't called upon

我正在 python 开发一款 Mastermind 游戏,我的 class 游戏之一叫做游戏 class。这将运行 'setup' 游戏(例如查找玩家数量、玩家名称等)。但是,当我尝试将名称附加到在我的游戏 class。这是我的游戏 class 目前的样子:

class WorldOfMastermind:
    """WOM class - Runs whole game, takes results from other classes and stores them (namely players and their scores)"""
    
    def __init__(self):
        self.__playerList = []
        self.__playerDetails = dict()
        
    def run(self):
        """Run method - holds the menu screen - allows for the user to add player, show scores, play game or quit"""
        cpuNames = ComputerPlayer.addDetails(self)
        self.__playerList.extend((cpuNames))
        start = input('What would you like to do?\n (r) register a new user\n (s) show the score board\n (p) play a game\n (q) quit\n')
        if start == 'r':
            tempName = HumanPlayer.addDetails(self)
            for i in self.__playerList:
                if i == tempName:
                    print('')
                    print('Sorry, that name already exists')
                    print('')
                    self.run()
            self.__playerList.append(tempName)
            self.__playerDetails[tempName] = [0, 0, float(0)]
            print('Welcome,', tempName + '!')
            print('')
            self.run()

        elif start == 's':
            self.printScoreBoard() 

        elif start == 'p':
            print('Let\'s play the game of Mastermind!')
            Game.startPrep(self, self.__playerList)

        elif start == 'q':
            print('Thank you for playing the World of Mastermind!')
            exit()
        else:
            print('\nSorry, that is an invalid input')
            print('')
            self.run()
    

    def printScoreBoard(self):
        """ Print the scoreboard by iterating through the dictionary """
        print('=====================================')
        print('Name             Score Games Average ')
        print('=====================================')
        for i in self.__playerDetails:
            print('{:<15} '.format(i), '{:>5}'.format(self.__playerDetails[i][0]), '{:>5}'.format(self.__playerDetails[i][1]), '{:>7}'.format(self.__playerDetails[i][2]))
        print('=====================================\n')   
        self.run()

class Game:
    """Begin the initialization of the game and return results to WOM class"""
    def __init__(self, playerCount):
        self.__playerCount = playerCount
        self.__playingList = []

    def startPrep(self, playerList):
        """Prepares the game"""
        Game.getPlayerCount(self)
        Game.getPlayerNames(self, playerList)

    def getPlayerCount(self):
        """Gathers the number of players"""
        while True:
            try:
                self.__playerCount = int(input('How many players (2-4)?'))
            except ValueError:
                print('Only numbers allowed')
            else:
                if self.__playerCount < 2 or self.__playerCount > 4:
                    print('Player count must be between 2-4 inclusive')
                else:
                    break
    
    def getPlayerNames(self, playerList):
        """Gathers names of players and puts them into a list"""
        while True:
            if self.__playerCount == 2:
                while True:
                    player1 = input('What is the name of player #1?')
                    if player1 in playerList:
                        print('successful')
                        self.__playingList.append(player1)
                        break
                    else:
                        print('Invalid username')

                while True:
                    player2 = input('What is the name of player #2?')
                    if player2 in playerList:
                        if player2 not in self.__playingList:
                            print('successful')
                            self.__playingList.append(player2)
                            break
                        else:
                            print(player2, 'is already in the game.')
                    else:
                        print('Invalid username')
            break

我觉得错误来自于调用 getPlayerNames 方法。由于它需要一个参数(来自另一个 class 的玩家列表),这纯粹是为了检查输入的名称是否确实在游戏中,如果是,它会打印 'successful' (它确实如此)但是当试图将输入的名称附加到新的 'playingList' 中会引发错误。我不确定为什么这是因为追加行不需要引用另一个 class 的属性。如有任何建议,我们将不胜感激!

你的代码适用于我,格式如下。如果我 运行 以下内容,它会询问玩家数量并确保你提供的名字在符合条件的玩家列表中。

class Game:
    """Begin the initialization of the game and return results to WOM class"""
    def __init__(self, playerCount=0):
        self.__playerCount = playerCount
        self.__playingList = []

    def startPrep(self, eligible):
        """Prepares the game"""
        self.getPlayerCount()
        self.getPlayerNames(eligible)

    def getPlayerCount(self):
        """Gathers the number of players"""
        while True:
            try:
                self.__playerCount = int(input('How many players (2-4)?'))
            except ValueError:
                print('Only numbers allowed')
            else:
                if self.__playerCount < 2 or self.__playerCount > 4:
                    print('Player count must be between 2-4 inclusive')
                else:
                    break

    def getPlayerNames(self, playerList):
        """Gathers names of players and puts them into a list"""
        for i in range(self.__playerCount):
            while True:
                s = 'What is the name of player #%d? '%(i+1)
                player = input(s)
                if player in playerList:
                    print('successful')
                    self.__playingList.append(player)
                    break
                else:
                    print('Invalid username')

g = Game()
g.startPrep(['bob','bill'])