-4

我刚刚编写了一个简单的 Python 类来创建一个列表列表(这将是一个井字游戏板),它给了我一个语法错误。

我已经将语法与许多其他类进行了比较,所有这些类都有效,并且所有这些类都具有相同的语法(据我所见)。

这是 Python 3.2。错误发生在代码的第二个冒号处,因此在构造函数声明之后(或者至少是红色突出显示的内容)。

class Board:
    def__init__(self, N):
        """Create a list of lists that will represent my playing board"""
        self._N = N
        Brd = []
        for i in range(N):
            Brd = Brd + ['()','()','()']
        self._theBoard = Brd

    def drawBoard(N):
        """Draws the Board"""
        print(self._theBoard)

提前致谢

4

4 回答 4

10

def您在and之间缺少一个空格__init__

def__init__(self, N):

将其添加到:

def __init__(self, N):

请注意,您的drawBoard(N)方法缺少self参数;N将在调用时设置为实例。

于 2012-12-21T11:52:54.543 回答
5

I guess you meant drawBoard to be a method in your Board class, in this case you need to pass self to it explicitly:

def drawBoard(self, N):
    """Draws the Board"""
    print(self._theBoard)

and it should be indented the same way as the other method. Note, that the N parameter is useless here.

Others already pointed missing space after def in your __init__ method definition.

于 2012-12-21T11:53:24.980 回答
3

你忘了在这里给一个空间:

def__init__(self, N):

应该

def __init__(self, N):
于 2012-12-21T11:53:12.337 回答
0

__init__ is the way to define constructor in python. You have to precede it by def to declare it is a function. Hence the correct way to do so is:

def __init__():

That is put space between def and init.

于 2012-12-21T11:58:33.063 回答