python-3.x - 如何修复代码中的缩进错误?
问题描述
我做了一个 tic/tac/toe 游戏,我不断收到关于如何有太多的制表符和空格混合的缩进错误,但我尝试逐行重新缩进,但它不起作用。我什至把它放在“Sublime Text”中,它会自动重新缩进或将空格变成制表符。它仍然没有工作。有没有人有任何建议,也许我遗漏了一些明显的错误,这把整个事情搞砸了?
解决方案
您的代码中有一些缩进错误,但我没有注意到混合制表符和空格的任何问题。相反,压痕深度不一致,并且在某些地方是不正确的。下面是对您的代码的清理,您应该能够将其复制并粘贴到文件中并运行:
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()
推荐阅读
- java - 如何在 jTable 中获取选中复选框的值并计算它们在 Java 中的平均值
- swift - SwiftUI:如何为当前动画的父母的孩子制作动画
- go - 在 gin 路由器中使用现有的会话 cookie
- android - 卡片角半径在 CardView 中不起作用
- flutter - 酒店客人一次登录扑腾
- html - 从下到上填充 SVG 心形
- java - SpringBoot 用户缺少权限或找不到对象 HSQLDB
- cmake - 如何构建 libobs 作为 obs-studio-node 模块的依赖项?
- java - 如何修复无法从静态上下文中引用的非静态变量
- azure-functions - Azure 函数的 CosmosDB 触发器