Skip to main content
 首页 » 编程设计

python之Python 错误中的缩进

2025年05月04日1898°冷暖

我是 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 
" 

请您参考如下方法:

如果这是你的完整代码,你不能这样做:

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""" 

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