python - 为什么它说“期望一个缩进块”?
问题描述
from tkinter import*
import random
import time
class Coords:
def __init__(self, x1=0, y1=0, x2=0, y2=0):
self.x1 = x1
self.y1 = y1
self.x2 = x2
self.y2 = y2
def within_x(co1, co2):
if co1.x1 > co2.x1 and co1.x1 < co2.x1:
return True
elif co1.x2 > co2.x1 and co1.x2 < co2.x2:
return True
elif co2.x1 > co1.x1 and co2.x1 < co1.x1:
return True
elif co2.x2 > co1.x1 and co2.x2 < co1.x1:
return True
else:
return False
def within_y(co1, co2):
if (co1.y1 > co2.y1 and co1.y1 < co2.y2) \
or (co1.y2 > co2.y1 and co1.y2 < co2.y2)\
or (co2.y1 > co1.y1 and co2.y1 < co1.y1) \
or (co2.y2 > co1.y1 and co2.y2 < co1.y1):
return True
else:
return False
def collided_right(co1, co2):
if wi1thin_y(co1, co2):
if co1.x2 >= co2.x1 and co1.x2 <= co2.x2:
return True
return False
class Game:
def __init__(self):
self.tk = Tk()
self.tk.title("Mr. Stick Man Races for the Exit")
self.tk.resizable(0, 0)
self.tk.wm_attributes("-topmost", 1)
self.canvas = Canvas(self.tk, width=500, height=500, \
highlightthickness=0)
self.canvas.pack()
self.tk.update()
self.canvas_height = 500
self.canvas_width = 500
self.bg = PhotoImage(file="backround.gif")
w = self.bg.width()
h = self.bg.height()
for x in range(0, 5):
for y in range(0, 5):
self.canvas.create_image(x * w, y * h, \
image=self.bg, anchor='nw')
self.sprites = []
self.Running = True
def mainloop(self):
while 1:
if self.Running == True:
for sprite in self.sprites:
sprite.move()
self.tk.update_idletasks()
time.sleep(0.01)
g = Game()
g.mainloop()
它说在第 30 行需要一个缩进块:
def within_y(co1, co2):
if (co1.y1 > co2.y1 and co1.y1 < co2.y2) \
or (co1.y2 > co2.y1 and co1.y2 < co2.y2)\
or (co2.y1 > co1.y1 and co2.y1 < co1.y1) \
or (co2.y2 > co1.y1 and co2.y2 < co1.y1):
return True
else:
return False
应该在哪里缩进?
解决方案
这里的 return 被称为 if 语句 ex 的下一行。if 语句:#这里是 if 语句 return True # 这里你调用的是 return
它应该像 If 语句:返回 true else 语句:返回 false
这是你的代码
推荐阅读
- visual-studio-code - 远程 ssh 键盘 - 与 vscode 交互
- laravel - Laravel Firebase OTP 身份验证/注册表单
- sql - 使用 case 语句动态选择要加入 Postgres 的表
- windows - Windows 中出现错误 - OSError: [Errno 22] Invalid argument - datetime
- c# - 以下 c# 代码中属性成人的堆栈溢出异常
- java - 构建 apk 和加载设备不起作用
- mysql - 将列日期时间值拆分为多列
- java - 递归查找二维数组中的单词
- element - vue-the-mask + element ui 输入槽 2 值错误
- translate - 将 MAGICAL 中的程序翻译成其他编程语言?