简体   繁体   中英

Indentation in Python error

Im new to python and trying to create a tictactoe game to learn the syntax and keep running into issues with indentation. My understanding is that as long as the whitespace is consistent then the program should run? Here is the code:

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

Im doing this in sublime. Any ideas as to why im getting this error:

"  File "test.py", line 27

    ^
IndentationError: expected an indented block
"

If this is your full code, you can't do this:

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

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

The functions should have some content. You can create empty functions, but at least one line should be written, for example:

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

EDIT

As stated by @SethMMorton , you can also do this without using pass , and keeping the same structure of your code, just changing

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

for

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

These are called docstrings, and are used to describe not only how to use functions but also to describre other things, like classes content and attributes. This way, you would get no identation error as well.

You need to specify contents in your functions or use pass to leave them unimplemented:

def getMove ():
    pass

Your function definition should have at least one code statement. Please note that you also defining getMove() twice and you should remove one of them.

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 expects a function definition after def getMove(): . As the error says, it expected an indented block, namely your function definition. If you don't want to define a function yet, you can use the pass statement, which is used when you need a statement for syntactical reasons, but you don't want to do anything (yet).

def getMove():
    pass

Not to forget to implement the function, raise NotImplementedError.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM