"Inconsistent Use of Tabs and Spaces in Indentation"

Inconsistent use of spaces and tabs in Python

Python is heavily reliant on consistent code indentation to identify code blocks, and the TabError exception occurs when you use a different mix of tabs and/or spaces to indent lines in the same code block.

You should check the tabs/spaces used to indent the line before the line of error:

    group_name = group['GroupName']

and make sure it has the same mix of tabs and spaces that are used to indent the line of error:

    print(group_name)

Moreover, it is generally discouraged to use tabs at all for indentation in Python precisely because it is easy to run into such a problem. I would recommend that you convert all your tabs to 4 spaces to be more easily able to spot such inconsistent indentation issues.

Inconsistent use of tabs and spaces in indentation

It means you have mixed up spaces and tabs in the indentation. You have to fix that to be consistent with either tabs or spaces.

What is inconsistent use of tabs and spaces in indentation?

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

TabError: inconsistent use of tabs and spaces in indentation in views - Django

This is not a Django but Python error, mixing Tabs and Spaces is not allowed in Python. You should be using either one of those and preferred indentation standard is Spaces of width 4 for each block.

Fix is pretty obvious, replace the Tabs with 4 space characters instead. You should try to configure your IDE/Editor to put 4 spaces whenever Tab is pressed and that should prevent any future occurrences of this.

Please read Python Style Guide for more details.



Related Topics



Leave a reply



Submit