【问题标题】:How do I fix the indentation error in my code?如何修复代码中的缩进错误?
【发布时间】:2019-04-24 22:29:24
【问题描述】:

我制作了一个 tic/tac/toe 游戏,但我不断收到缩进错误,提示标签和空格的混合过多,但我尝试逐行重新缩进,但没有成功。我什至把它放在“Sublime Text”中,它会自动重新缩进或将空格变成制表符。它仍然没有工作。有没有人有任何建议,也许我遗漏了一些明显的错误,导致整个事情变得混乱?

【问题讨论】:

  • 您是否尝试过此处列出的解决方案:stackoverflow.com/questions/44803547/autoindent-on-sublime-text
  • 是的。它不起作用。我不确定是什么问题,或者我做错了。因为我有很多函数,它只会在每个函数内部缩进,这当然不是我想要的。我不得不去每个功能重新发明一切,但它仍然没有工作

标签: python-3.x turtle-graphics


【解决方案1】:

您的代码中有一些缩进错误,但我没有注意到混合制表符和空格有任何问题。相反,压痕深度不一致,并且在某些地方是不正确的。下面是对您的代码的清理,您应该可以将其复制并粘贴到文件中并运行:

from turtle import *

# draw board
pieces = ["", "", "", "", "", "", "", "", ""]
turn = "X"

setup(600, 600)
bgcolor("black")

pencolor("white")
hideturtle()
speed('fastest')
pensize(10)
penup()

# Horizontal bars
goto(-300, 100)
pendown()
forward(600)
penup()
goto(-300, -100)
pendown()
forward(600)
penup()

# Vertical bars
goto(-100, 300)
setheading(-90)
pendown()
forward(600)
penup()
goto(100, 300)
pendown()
forward(600)
penup()

pencolor("green")

# Draw noughts and crosses
def cross(x, y):
    penup()
    goto(x + 20, y - 20)
    setheading(-45)
    pendown()
    forward(226)
    penup()
    goto(x + 180, y - 20)
    setheading(-135)
    pendown()
    forward(226)
    penup()

def nought(x, y):
    penup()
    goto(x + 100, y - 180)
    setheading(0)
    pendown()
    circle(80)
    penup()

def drawPieces(pieces):
    x, y = -300, 300

    for piece in pieces:
        if piece == "X":
            cross(x, y)
        elif piece == "O":
            nought(x, y)

        x += 200
        if x > 100:
            x = -300
            y -= 200

def clicked(x, y):
    global turn, pieces

    onscreenclick(None)  # disable handler when inside handler!

    column = (x + 300) // 200
    row = (y - 300) // -200
    square = int(row * 3 + column)

    print("You clicked ", x, ",", y, " which is square ", square)

    if pieces[square] == "":
        pieces[square] = turn

        if turn == "X":
            turn = "O"
        else:
            turn = "X"

        drawPieces(pieces)
    else:
        print("That square is already taken")

    onscreenclick(clicked)

# Start the game
onscreenclick(clicked)

mainloop()

【讨论】:

  • @AngelBaby,问一下——我会尽力而为。
猜你喜欢
  • 2014-10-22
  • 2011-04-03
  • 2022-11-21
  • 2021-08-11
  • 2019-04-07
  • 2014-05-01
  • 2019-08-25
  • 2022-06-10
  • 2018-05-05
相关资源
最近更新 更多