Python 缩进中制表符和空格的用法不一致是什么?

Python 缩进中制表符和空格的用法不一致是什么?,python,python-3.x,Python,Python 3.x,我正在制作一个python程序,它读取图像并根据每个像素的颜色创建一个级别。 尽管如此,当我运行它时,在第139行的缩进中使用制表符和空格时会出现这个错误。这很令人沮丧,因为我不明白为什么它是错的 我已经尝试将缩进大小更改为2、4和8,但没有解决任何问题 这是我的密码 #Libraries import Image from os import system from random import randint #Object class class object: def __ini

我正在制作一个python程序,它读取图像并根据每个像素的颜色创建一个级别。 尽管如此,当我运行它时,在第139行的缩进中使用制表符和空格时会出现这个错误。这很令人沮丧,因为我不明白为什么它是错的

我已经尝试将缩进大小更改为2、4和8,但没有解决任何问题

这是我的密码

#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")

可以使用空格或制表符进行缩进。但你不能把它们混在一起。如果使用空格,则每次都需要使用相同数量的空格

你能试试吗

#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")

在整个代码中必须保持缩进不变

对象类 类对象: 定义初始自我,x,y,面向,图像,类型: self.x=x self.y=y 自我面对 self.image=image self.type=type 这里的缩进是4个空格,但是

创建像素阵列 对于y的范围高度: 对于范围宽度中的x: 像素。appendpicload[x,y] 这里有2个空格作为缩进,这是错误的。
您应该使用制表符使缩进在任何地方都保持不变。

这意味着某些行使用空格缩进,而另一些行使用制表符缩进。错误是不言自明的。只要看一下最后几行代码,就有一行是带有2个空格缩进的for循环,最后一行是带有4个空格缩进的for循环……好的,有没有快速的方法来解决这个问题?始终使用相同的缩进。大多数IDE会在需要时自动为您完成这项工作。即使Python的空闲也会这样做that@Tomerikoo如果一个循环使用2-空格缩进,而另一个循环使用4-空格缩进,这不是一个错误。我不认为至少我不认为是因为我在一个ide中使用了部分缩进,而在另一个IDEON中使用了部分缩进,可以混合制表符和空格,但它们必须是明确的。可以在制表符和空格缩进块之间切换。不允许使用制表符继续空格缩进块,反之亦然。@MisterMiyagi有趣,我不知道这一点。。。您是否有引用?类对象->类对象,这不是错误。这只是意味着你的类名是object而不是ObjectOh我知道,我是在评论命名风格。