-2

I am making a python program that reads an image and creates a level according to every pixel's color. 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.

I've already tried changing the indentation size to 2, 4 and 8 but it fixed nothing

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")
Mews
  • 15
  • 6
  • This means that some lines are indented using spaces and others using tab characters. – Maximouse Feb 15 '20 at 18:22
  • 3
    The error is very much self explanatory. Just looking at your last few lines of code, you have one `for` loop with 2-space indent and the last one is with 4-space indents... – Tomerikoo Feb 15 '20 at 18:23
  • ok is there any quick way to solve it? – Mews Feb 15 '20 at 18:26
  • Use the same indents all the time. Most IDEs do that for you automatically whenever is needed. Even Python's IDLE does that – Tomerikoo Feb 15 '20 at 18:32
  • @Tomerikoo It's *not* an error if one loop uses 2-space indent and another uses 4-space indent. – Kelly Bundy Feb 15 '20 at 18:58
  • Although sometimes Python _will_ let you mix tabs and spaces, it can be confusing (see [Why is this else: pass needed for processing to continue?](https://stackoverflow.com/questions/14900821/why-is-this-else-pass-needed-for-processing-to-continue)), so I strongly suggest you avoid doing it. – martineau Feb 15 '20 at 20:12
  • Does this answer your question? ["inconsistent use of tabs and spaces in indentation"](https://stackoverflow.com/questions/5685406/inconsistent-use-of-tabs-and-spaces-in-indentation) – AMC Feb 16 '20 at 01:58

2 Answers2

0

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")
Arco Bast
  • 3,595
  • 2
  • 26
  • 53
  • i didn't at least i dont think so – Mews Feb 15 '20 at 18:25
  • thanks i think what caused it is that i made part of it in one ide and another in other ide – Mews Feb 15 '20 at 18:42
  • 1
    One *can* mix tabs and spaces, but they must be unambiguous. Switching between tab and space indented blocks is fine. *Continuing* a space I indented block with tabs or vice versa is not allowed. – MisterMiyagi Feb 15 '20 at 18:55
  • @MisterMiyagi interesting, I was not aware of this ... do you have a reference? – Arco Bast Feb 15 '20 at 19:27
0

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

#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. You should just use tab to keep indentation same everywhere.

AMC
  • 2,642
  • 7
  • 13
  • 35
Medhavi Monish
  • 329
  • 2
  • 8