【问题标题】:Python 3 IndentationErrorPython 3 缩进错误
【发布时间】:2016-08-19 23:12:42
【问题描述】:

尽管看到很多关于python缩进错误的帖子并尝试了很多解决方案,我仍然得到这个错误:

import random
import copy
import sys
the_board = [" "]*10
def printboard(board):
    print(board[7] + " " + "|" + board[8] + " " + "|" + board[9])
    print("--------")
    print(board[4] + " " + "|" + board[5] + " " + "|" + board[6])
    print("--------")
    print(board[1] + " " + "|" + board[2] + " " + "|" + board[3])

def choose_player():
    while True:
        player = input("What do you want to be; X or O?")
        if player == "X":
            print("You chose X")
            comp = "O"
            break
        elif player == "O":
            print("You chose O")
            comp = "X"
            break
        else:
            print("Invalid Selection")
            continue
    return [player, comp]


def virtual_toss():
    print("Tossing to see who goes first.....")
    x = random.randint(0,1)
    if x== 0:
        print("AI goes first")
        move = "comp"
    if x == 1:
        print("Human goes first")
        move = "hum"
    return move
def win(board,le):
    if (board[7] == le and board[8] == le and board[9]==le) or (board[4] == le and board[5]==le and board[6] == le)or (board[1] == le and board[2]==le and board[3] == le)or (board[7] == le and board[5]==le and board[3] == le)or (board[9] == le and board[5]==le and board[1] == le)or (board[7] == le and board[4]==le and board[1] == le)or (board[8] == le and board[5]==le and board[2] == le)or (board[9] == le and board[6]==le and board[3] == le):
        return True
    else:
        return False
def make_move(board,number,symbol):
     board[number] = symbol
def board_full(board):
    count = 0
    for item in board:
        if item in ["X","O"]:
            count+= 1
    if count ==9 :
        return True
    else:
        return False
def valid_move(board,num):
    if board[num] == " ":
        return True
    else:
        return False
def player_move(board):
    number = int(input("Enter the number"))
    return number
def copy_board(board):
    copied_board = copy.copy(board)
    return copied_board
def check_corner(board):
    if (board[7] == " ") or (board[9] == " ") or (board[1] == " ") or (board[3] == " "):
        return True
    else:
        return False
def check_center(board):
    if (board[5] == " "):
        return True
    else:
        return False


while True:
    count = 0
    loop_break = 0
    print("welcome to TIC TAC TOE")
    pla,comp = choose_player()
    turn = virtual_toss()
    while True:
        printboard(the_board)
        if turn == "hum":
            if board_full() == True:
                again = input ("Game is a tie. Want to try again? Y for yes and N for No")
                if again == "Y":
                    loop_break = 6
                    break
                else:
                    system.exit()
    if loop_break == 6:
        continue 


        while True:
                number = player_move(the_board)
                if (valid_move(the_board,number) == True) and not(board_full == False):
                    make_move(the_board,number,pla)
                    break
                else:
                    print("Invalid Move, try again!")
                    continue
             if (win(the_board,pla) == True):
                print ("Yay, you won!!!")
                printboard(the_board)
                count = 1
                break
            else:
                turn = "comp"
                printboard(the_board)
                continue

        else:
            if board_full() == True:
                again = input ("Game is a tie. Want to try again? Y for yes and N for No")
                if again == "Y":
                    loop_break = 6
                    break
                else:
                    system.exit()
    if loop_break = 6:
        continue
            copy_board(the_board)
                for i in range(0,9):
                    make_move(copied_board,i,pla)
                    if(win(copied_board,pla) == True):
                        make_move(the_board,comp)
                        turn = "hum"
                        loop_break = 1
                        break
                    else:
                        continue
                if loop_break = 1:
                    continue
                if (check_corner(the_board) == True) or (check_center(the_board)==True):
                    for i in [7,9,1,3,5]:
                        if(valid_move(copied_board,i)==True):
                            make_move(copied_board,i,comp)
                            if(win(copied_board,comp)==True):
                                make_move(the_board,i,comp)
                                print("The AI beat you")
                                loop_break = 2
                                count = 1
                                break
                            else:
                                make_move(the_board,i,comp)
                                turn = "hum"
                                loop_break = 3
                                break
                if loop_break == 2:
                    break
                elif loop_break == 3:
                    continue
                else:
                    for i in [8,4,6,2]:
                        if(valid_move(copied_board,i)==True):
                            make_move(copied_board,i,comp)
                            if(win(copied_board,comp)):
                                make_move(the_board,i,comp)
                                print("The AI beat you")
                                count = 1
                                loop_break = 4
                                break
                            else:
                                make_move(the_board,i,comp)
                                turn = "hum"
                                loop_break = 5
                                break
                if loop_break == 4:
                    break
                elif loop_break == 5:
                    continue

    if count == 1:
        again = input("Would you like to play again? y/n")
        if again == "y":
            continue
        else:
            system.exit()

我知道这是一个相当长的代码,但我会指出错误所在的特定区域。你看,我收到以下错误消息:

    Traceback (most recent call last):
  File "python", line 106
    if (win(the_board,pla) == True):
                                   ^
IndentationError: unindent does not match any outer indentation level

更具体地说,这部分代码:

while True:
                number = player_move(the_board)
                if (valid_move(the_board,number) == True) and not(board_full == False):
                    make_move(the_board,number,pla)
                    break
                else:
                    print("Invalid Move, try again!")
                    continue
             if (win(the_board,pla) == True):
                print ("Yay, you won!!!")
                printboard(the_board)
                count = 1
                break

有什么帮助吗?我无法让代码以任何方式工作!!

【问题讨论】:

  • 看代码。明显缩进错了!看看它怎么不排队?
  • 末尾附近的while True: 行是错误的。它与上面的continue 处于同一级别。但是continue 会跳回到循环的开头,所以之后什么都不会执行。

标签: python python-3.x indentation


【解决方案1】:

尝试将前七行代码取消缩进一级(while 循环中的代码)。这应该使缩进正常工作。此外,if 语句之前似乎有一个额外的空格会导致您出错。也删除这个空间。这将使这部分代码如下:

while True:
        number = player_move(the_board)
        if (valid_move(the_board,number) == True) and not(board_full == False):
            make_move(the_board,number,pla)
            break
        else:
            print("Invalid Move, try again!")
            continue
        if (win(the_board,pla) == True):
            print ("Yay, you won!!!")
            printboard(the_board)
            count = 1
            break

【讨论】:

    【解决方案2】:

    您需要一致地缩进while (True): 下的块。例如:

    while True:
        number = player_move(the_board)
        if (valid_move(the_board,number) == True) and not(board_full == False):
            make_move(the_board,number,pla)
            break
        else:
            print("Invalid Move, try again!")
                continue
    
        if (win(the_board,pla) == True):
            print ("Yay, you won!!!")
            printboard(the_board)
            count = 1
            break
        else:
            turn = "comp"
            printboard(the_board)
            continue
    

    【讨论】:

      【解决方案3】:

      问题是,if 语句是代码其余部分的缩进。您想对齐所有代码,错误应该会消失。

      while True:
           number = player_move(the_board)
           if (valid_move(the_board,number) == True) and not(board_full == False):
               make_move(the_board,number,pla)
               break
           else:
              print("Invalid Move, try again!")
              continue
           if (win(the_board,pla) == True):
              print ("Yay, you won!!!")
              printboard(the_board)
              count = 1
              break
      

      【讨论】:

        猜你喜欢
        • 2020-12-17
        • 1970-01-01
        • 1970-01-01
        • 2014-09-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多