[英]What is inconsistent use of tabs and spaces in indentation?
I am making a python program that reads an image and creates a level according to every pixel's color.我正在制作一个 python 程序,它读取图像并根据每个像素的颜色创建一个级别。 Although , when i run it, I'm getting this error "inconsistent use of tabs and spaces in indentation" at line 139. It's quite frustrating because I can't figure out why it is wrong.
虽然,当我运行它时,我在第 139 行收到此错误“在缩进中使用制表符和空格不一致”。这非常令人沮丧,因为我无法弄清楚为什么它是错误的。
I've already tried changing the indentation size to 2, 4 and 8 but it fixed nothing我已经尝试将缩进大小更改为 2、4 和 8,但它没有解决任何问题
Here's my code这是我的代码
#Libraries
import Image
from os import system
from random import randint
#Object class
class object:
def __init__(self, x , y, facing, image, type):
self.x = x
self.y = y
self.facing = facing
self.image = image
self.type = type
#Game variables
entities = []
score = 0
pixels = []
#Read image
pic = Image.open("level1.png", mode="r")
#Get image size
width = pic.size[0]
height = pic.size[1]
picload = pic.load()
#Create pixel array
for y in range(height):
for x in range(width):
pixels.append(picload[x, y])
#Read all pixels
x = 0
y = 0
for i in range(len(pixels)):
#Change x y coordinates
x += 1
if x % height == 0:
x = 0
y += 1
pixel = pixels[i]
r = pixel[0]
g = pixel[1]
b = pixel[2]
a = pixel[3]
#Create object based on pixel info
#If pixel is black
if r >= 200 and g >= 200 and b >= 200: entities.append(x, y, "r", "■", "wall")
#If pixel is red
elif r >= 200 and g <= 50 and b <= 50: entities.append(x, y, "r", "•", "food")
#if pixel is blue
elif r <= 50 and g <= 50 and b >= 200: entities.append(x, y, "r", "☺", "player")
#Running the level
while True:
#output
print("Output:", output)
output = ""
print("Score:", score)
for y in range(height):
print("")
for x in range(width):
drawed = False
for i in range(len(entities)):
entity = entities[i]
if entity.x == x and entity.y == y and drawed == False:
drawed = True
print(entity.image, end = "")
if not drawed: print(".", end="")
#input
dir = input("\n\n####################\nWASD: ").lower()
#processing
#movement
for i in range(len(entities)):
entity = entities[i]
ii = i
if entity.type == "player":
if dir == "w":
entity.y -= 1
entity.facing = "u"
elif dir == "s":
entity.y += 1
entity.facing = "d"
elif dir == "a":
entity.x -= 1
entity.facing = "l"
elif dir == "d":
entity.x += 1
entity.facing = "r"
for h in range(len(entities)):
centity = entities[h]
if entity.x == centity.x and entity.y == centity.y and not ii == h:
if dir == "w":entity.y += 1
elif dir == "s":entity.y -= 1
elif dir == "a":entity.x += 1
elif dir == "d": entity.x -=1
#colisions
for h in range(len(entities)):
entity = entities[h]
f = entity.facing
cx = entity.x
cy = entity.y
if f == "r": cx += 1
elif f == "l": cx -= 1
elif f == "u": cy -= 1
elif f == "d": cy += 1
for i in range(len(entities)):
centity = entities[i]
if centity.x == cx and centity.y == cy:
print("a")
#Clear terminal
system("clear")
You can either use spaces or tabs for indentation.您可以使用空格或制表符进行缩进。 But you cannot mix them.
但是你不能混合它们。 If you use spaces, you need to use the same amount of spaces every time.
如果使用空格,则每次都需要使用相同数量的空格。
Can you try:你能试一下吗:
#Libraries
import Image
from os import system
from random import randint
#Object class
class object:
def __init__(self, x , y, facing, image, type):
self.x = x
self.y = y
self.facing = facing
self.image = image
self.type = type
#Game variables
entities = []
score = 0
pixels = []
#Read image
pic = Image.open("level1.png", mode="r")
#Get image size
width = pic.size[0]
height = pic.size[1]
picload = pic.load()
#Create pixel array
for y in range(height):
for x in range(width):
pixels.append(picload[x, y])
#Read all pixels
x = 0
y = 0
for i in range(len(pixels)):
#Change x y coordinates
x += 1
if x % height == 0:
x = 0
y += 1
pixel = pixels[i]
r = pixel[0]
g = pixel[1]
b = pixel[2]
a = pixel[3]
#Create object based on pixel info
#If pixel is black
if r >= 200 and g >= 200 and b >= 200: entities.append(x, y, "r", "■", "wall")
#If pixel is red
elif r >= 200 and g <= 50 and b <= 50: entities.append(x, y, "r", "•", "food")
#if pixel is blue
elif r <= 50 and g <= 50 and b >= 200: entities.append(x, y, "r", "☺", "player")
#Running the level
while True:
#output
print("Output:", output)
output = ""
print("Score:", score)
for y in range(height):
print("")
for x in range(width):
drawed = False
for i in range(len(entities)):
entity = entities[i]
if entity.x == x and entity.y == y and drawed == False:
drawed = True
print(entity.image, end = "")
if not drawed: print(".", end = "")
#input
dir = input("\n\n####################\nWASD: ").lower()
#processing
#movement
for i in range(len(entities)):
entity = entities[i]
ii = i
if entity.type == "player":
if dir == "w":
entity.y -= 1
entity.facing = "u"
elif dir == "s":
entity.y += 1
entity.facing = "d"
elif dir == "a":
entity.x -= 1
entity.facing = "l"
elif dir == "d":
entity.x += 1
entity.facing = "r"
for h in range(len(entities)):
centity = entities[h]
if entity.x == centity.x and entity.y == centity.y and not ii == h:
if dir == "w":entity.y += 1
elif dir == "s":entity.y -= 1
elif dir == "a":entity.x += 1
elif dir == "d": entity.x -=1
#colisions
for h in range(len(entities)):
entity = entities[h]
f = entity.facing
cx = entity.x
cy = entity.y
if f == "r": cx += 1
elif f == "l": cx -= 1
elif f == "u": cy -= 1
elif f == "d": cy += 1
for i in range(len(entities)):
centity = entities[i]
if centity.x == cx and centity.y == cy:
print("a")
#Clear terminal
system("clear")
You have to keep indentation same through out the code您必须在整个代码中保持缩进相同
#Object class
class object:
def __init__(self, x , y, facing, image, type):
self.x = x
self.y = y
self.facing = facing
self.image = image
self.type = type
Here indentation is 4 spaces, but这里的缩进是 4 个空格,但是
#Create pixel array
for y in range(height):
for x in range(width):
pixels.append(picload[x, y])
here you have 2 space as indentation , this was the error.这里有 2 个空格作为缩进,这是错误。 You should just use tab to keep indentation same everywhere.
您应该只使用制表符来保持所有地方的缩进相同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.