Type Object没有属性Python

时间:2015-10-02 03:46:12

标签: python object python-3.x attributes

我已经很久没有在python中编码,因为2.7出来了。我试图做一个简单的tic tac toe游戏,但是如果我尝试在TicTacToe对象中引用一个属性。它抛出一个运行时异常,说TicTacToe没有这样的属性。有什么想法吗?

class TicTacToe(object):

def __init__(self):
    '''
    :return: TicTacToe object
    '''
    self.turn = 1
    self.deckList = [Deck()]
    self.player1 = Player()
    self.player2 = Player()
def validateInput(self, num):
    if type(num) is not int:
        print('Not an integer 0-8')
        return 
    elif num < 0 | num > 8:
        print('Not between 0-8')
        return

def getUserInput(self, turn):
    if turn == 1:
        choice = input('Player 1: Enter 0-8\n')
        #turn = 2
    elif turn == 2:
        choice = input('Player 2: Enter 0-8\n')
        #turn = 1
    return choice

def startGame(self):
    player1Name = input('Player 1: Enter your name\n')
    player2Name = input('Player 2: Enter your name\n')
    self.player1.Player.setName(player1Name)
    self.player2.Player.setName(player2Name)
    player1Mark = input('Player 1: Enter your mark\n')
    player2Mark = input('Player 2: Enter your mark\n')
    if player1Mark == player2Mark:
        print('You both cannot have the same mark\n')
        return
    else:
        self.player1.setMark(player1Mark)
        self.player2.setMark(player2Mark)

    while True:  # Main loop
        choice = self.getUserInput(self.turn)

主要

from TicTacToe import TicTacToe
if __name__ == '__main__':
    game = TicTacToe
    game.startGame(game)
    pass

我在这里有其他课程,Player class和Deck(Board)class

import numpy as np

class Deck(object):
    def __init__(self):
        """
        :return: Deck
        """
        board = np.matrix([-1,-1,-1],[-1,-1,-1],[-1,-1,-1])
        player1Choices = []
        player2Choices = []

    def __str__(self):
        return self.board

class Player(object):

def __init__(self):
    """
    :return: A new Player
    """
    name = None
    mark = None
    statistics = [0, 0, 0]  # [Win, Tie, Lost]

def setName(self, name):
    """
    :param name: Set name of player
    """
    self.name = name
def setMark(self, mark):
    """
    :param mark: Set mark of player
    """
    self.mark = mark
def getScore(self):
    """
    :return: Statistics of player
    """
    score = ((self.statistics[0] * 2) + self.statistics[1] - self.statistics[2])
    return score
def __str__(self):
    """
    :return: String representation of values inside Player
    """
    return "Name: " + self.name + "\nMark: " + self.mark + "\nScore: " + self.getScore() + "\n"
def __cmp__(self, player):
    """
    :param: A Player 
    :return: Winner
    """
    if self.getScore() > player.getScore():
        return self
    elif self.getScore() < player.getScore():
        return player
    else:
        return None

1 个答案:

答案 0 :(得分:0)

game = TicTacToe

构造函数需要parens(和可选参数)。

game = TicTacToe()