Python中的缩进错误
我是python新手,正在尝试创建一个tictactoe游戏来学习语法,并不断遇到缩进问题。我的理解是,只要空格是一致的,那么程序应该运行吗?代码如下:Python中的缩进错误,python,Python,我是python新手,正在尝试创建一个tictactoe游戏来学习语法,并不断遇到缩进问题。我的理解是,只要空格是一致的,那么程序应该运行吗?代码如下: board = [ ['|', '|', '|' ], ['|', '|', '|'], ['|', '|', '|'] ] player1 = '' player2 = '' def tictactoe (): player1 = r
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
"
如果这是您的完整代码,则无法执行以下操作:
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
为了
这些被称为docstring,不仅用于描述如何使用函数,还用于描述其他事物,如类、内容和属性。这样,您也不会出现识别错误。如果这是您的完整代码,则无法执行以下操作:
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
为了
这些被称为docstring,不仅用于描述如何使用函数,还用于描述其他事物,如类、内容和属性。这样,也不会出现识别错误。您需要在函数中指定内容,或使用
pass
使其未实现:
def getMove ():
pass
您需要在函数中指定内容,或使用
pass
使其未实现:
def getMove ():
pass
函数定义至少应有一条代码语句。请注意,您还定义了两次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
函数定义至少应有一条代码语句。请注意,您还定义了两次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
Python希望函数定义在
def getMove():
之后。正如错误所说,它需要一个缩进块,即您的函数定义。如果您还不想定义一个函数,可以使用pass
语句,当出于语法原因需要一个语句,但您还不想做任何事情时,可以使用该语句
Python希望函数定义在
def getMove():
之后。正如错误所说,它需要一个缩进块,即您的函数定义。如果您还不想定义一个函数,可以使用pass
语句,当出于语法原因需要一个语句,但您还不想做任何事情时,可以使用该语句
不要忘记实现该函数,请引发NotImplementedError
def getMove():
"""get move and pass it to testMove"""
raise NotImplementedError
不要忘记实现该函数,请引发NotImplementedError
def getMove():
"""get move and pass it to testMove"""
raise NotImplementedError
请阅读Python样式指南。缩进应该是4个空格,并且应该避免使用制表符,因为不同的编辑器以不同的方式处理它们(例如,升华允许您指定一个制表符占用多少空格),而Python解释器将它们视为8个空格。为了避免混淆和无意义的错误,请使用4个空格。它看起来不错,可读性好,并且与其他人的做法一致。缩进应该是4个空格,并且应该避免使用制表符,因为不同的编辑器以不同的方式处理它们(例如,升华允许您指定一个制表符占用多少空格),而Python解释器将它们视为8个空格。为了避免混淆和无意义的错误,请使用4个空格。它看起来很漂亮,可读性好,并且与其他人的做法基本一致。或者,如果注释被替换为正确缩进级别的注释,它将在没有
传递的情况下工作。我主张OP应该这样做,因为doString应该用来描述如何使用函数及其意图,而不是注释。@SethMMorton观察得很好!我会把它添加到答案中。或者,如果注释被替换为正确缩进级别的a,那么它将在没有通过的情况下工作。我主张OP应该这样做,因为doString应该用来描述如何使用函数及其意图,而不是注释。@SethMMorton观察得很好!我会在答案中加上这个。谢谢。有没有办法进行语法提示或错误检查?有sublime或iPython的包裹吗?我不知道。成为一名优秀程序员的一部分是能够调试和理解错误代码。像这样的错误是一个很好的指示,可以在代码行前、行中和行后查看代码中的任何错误。如果这对您有任何帮助,请在我的答案旁边用复选框标记为答案。谢谢。有没有办法进行语法提示或错误检查?有sublime或iPython的包裹吗?我不知道。成为一名优秀程序员的一部分是能够调试和理解错误代码。像这样的错误是一个很好的指示,可以在代码的行前、行中和行后查看代码中的任何错误。如果这对您有任何帮助,请在我的答案旁边用复选框标记为答案。