缩进Python错误

时间:2015-04-18 23:24:45

标签: python

我是python的新手,并尝试创建一个tictactoe游戏来学习语法并继续遇到缩进问题。我的理解是,只要空格是一致的,那么程序应该运行?这是代码:

board = [ 
            ['|', '|', '|' ],
            ['|', '|', '|'],
            ['|', '|', '|'] 
        ] 

player1 = ''
player2 = ''


def tictactoe ():

    player1 = raw_input('What is player one name?')
    print(player1)
    player2 = raw_input('What is player two name?')
    print(player2)



def getMove ():
# get move and pass it to testMove

def getMove ():
# get move and pass it to testMove

# def testMove():
# test for moves and pass to make move

# def makeMove ():
#make move and loop back to getMove

我在崇高中这样做。关于为什么我得到这个错误的任何想法:

"  File "test.py", line 27

    ^
IndentationError: expected an indented block
"

5 个答案:

答案 0 :(得分:5)

如果这是您的完整代码,则无法执行此操作:

def getMove ():
# get move and pass it to testMove

# def testMove():
# test for moves and pass to make move

这些功能应该有一些内容。您可以创建空函数,但至少应写入一行,例如:

def getMove():
#get move and pass it to testMove
    pass

修改

如@SethMMorton所述,您也可以不使用pass并保持相同的代码结构,只需更改

def getMove():
#get move and pass it to testMove
    pass

def getMove():
    """Get move and pass it to testMove"""

这些被称为文档字符串,不仅用于描述如何使用函数,还用于描述其他内容,如类内容和属性。这样,您也不会遇到身份错误。

答案 1 :(得分:1)

您需要在函数中指定内容或使用pass使其未实现:

def getMove ():
    pass

答案 2 :(得分:0)

您的函数定义应至少包含一个代码语句。请注意,您还要定义两次getMove(),然后删除其中一个。

board = [ 
        ['|', '|', '|' ],
        ['|', '|', '|'],
        ['|', '|', '|'] 
    ] 

player1 = ''
player2 = ''


def tictactoe ():

   player1 = raw_input('What is player one name?')
   print(player1)
   player2 = raw_input('What is player two name?')
   print(player2)



def getMove ():
   pass

答案 3 :(得分:0)

Python期望def getMove():之后的函数定义。正如错误所说,它期望一个缩进的块,即你的函数定义。如果您还不想定义一个函数,可以使用pass语句,该语句在出于语法原因需要语句时使用,但您不想做任何事情(但是)。

def getMove():
    pass

答案 4 :(得分:0)

不要忘记实现该函数,引发NotImplementedError。

def getMove():
    """get move and pass it to testMove"""
    raise NotImplementedError