Python: 缩进错误

2 投票
2 回答
8192 浏览
提问于 2025-04-16 23:58

我表弟最近拿到了一本Python书,正在努力学习Python。他找我帮忙,但我有点搞不定。

他在用IDLE这个工具,报告说遇到了一个错误:

               Error: Inconsistent indentation detected!

    1)      Your indentation is outright incorrect (easy to fix), OR
    2)      Your indentation mixes tabs and spaces.

                   To fix case 2, change all tabs to spaces by using Edit->Select All
                   Followed by Format->Untabify Region and specify the number of
                   Columns used by each tab.

他把他的代码发给我看,缩进是正确的。不过代码运行不起来是因为有几个语法错误,但这不是重点。他说他用过“格式->去掉制表符”这个功能,但问题还是没解决。

我真搞不懂为什么我能运行他的Python文件,而他却不行。有没有人知道这是怎么回事?可惜我现在离他有五个小时的飞机距离,不然我可以亲自去看看。

他的代码在这里,我运行的时候没有缩进错误,所以我怀疑这对你们没有什么帮助。如果有问题的话,可能在我拿到代码的时候就已经被修复了。

import pygame, sys, random

skier_images = ["skier_down.png", "skier_right1.png",
                "skier_right2.png", "skier_left2.png",
                "skier_left1.png"]

class SkierClass(pygame.sprite.Sprite):
    def __init__(self)
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("skier_down.png")
        self.rect = self.image.get_rect()
        self.rect.center = [320, 100]
        self.angle = 0
    def turn(self, direction):
        self.angle = self.angle + direction
        if self.angle < -2: self.angle = -2
        if self.angle >  2: self.angle =  2
        center = self.rect.center
        self.image = pygame.image.load(skier_images[self.angle])
        self.rect = self.image.get_rect()
        self.rect.center = center
        speed = [self.angle, 6 - abs(self.angle) * 2]
        return speed
    def move(self, speed):
        self.rect.centerx = self.rect.centerx + speed [0]
        if self.rect.centerx < 20: self.rect.centerx =20
        if self.rect.centerx > 620: self.rect.centerx = 620

class ObstacleClass(pygame.sprite.Sprite):
    def __init__(self, image_file, location, type):
        pygame.sprite.Sprite.__init__(self)
        self.image_file = image_file
        self.image = pygame.image.load(image_file)
        self.location = location
        self.rect = self.image.get_rect()
        self.rect.center = location
        self.type = type
        self.passed = False

    def scroll(self, t_ptr):
        self.rect.centery = self.location[1] - t_ptr

def create_map(start, end):
    obstacles = pygame.sprite.Group()
    gates = pygame.sprite.Group()
    locations = []
    for i in range(10):
        row = random.randint(start, end)
        col = random.randint(0, 9)
        location = [col * 64 + 20, row * 64 + 20]
        if not (location in locations):
            locations.append(location)
            type = random.choice(["tree", "flag"])
            if type == "tree": img = "skier_flag.png"
            obstacle = ObstacleClass(img, location, type)
            obstacles.add(obstacle)
    return obstacles
def animate():
    screen.fill(255, 255, 255])
    pygame.display.update(obstacles.draw(screen))
    screen.blit(skier.image, skier.rect)
    screen.blit(score_text, [10, 10])
    pygame.display.flip()
def updateObstaclegroup(map0, map1):
    obstacles = pygame.sprite.Group()
    for ob in map0:  obstacles.add(ob)
    for ob in map1:  obstacles.add(ob)
    return obstacles

pygame.init()
screen = pygame.display.set_mode([640,640])
clock = pygame.time.Clock()
skier =SkierClass()
speed = [0, 6]
map_position = 0
points = 0
map0 = create_map(20, 29)
map1 = create_map(10, 19)
activeMap = 0
obstacles = updateObstacleGroup(map0, map1)
font = pygame.font.Font(None, 50)


while True:
    clock.tick(30)
    for event in pygame.event.get():
        if event.type == pygame.QUIT: sys.exit()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                speed = skier.turn(-1)
            elif efvent.key == pygame.K_RIGHT:
                speed = skier.turn(1)
    skier.move(speed)
    map_position += speed[1]

    if map_position >=640 and activeMap == 0:
        activeMap = 1
        map0 = create)map(20, 29)
        obstacles = updateObstacleGroup (map0, map1)
    if map_position >=1280 and activeMap ==1:
        activeMap = 0
        for ob in map0:
                ob.location[1] = ob.location[1] - 1280
        map_position = map_position - 1280
        map1 = create_map(10, 19)
        obstacles = updateObstacleGroup (map0, map1)

    for obstacle in obstacles:
                obstacle.scroll(map_position)
    hit = pygame.sprite.spritecollide(skier, obstacles, False)
    if hit:
        if hit[0].type == "tree" and not hit[0].passed:
            points = points - 100
            skier.image = pygame.image.load("skier_crash.png")
            animate()
            pygame.time.delay(1000)
            skier.image = pygame.image.load("skier_down.png")
            speed = [0, 6]
            hit[0].passed = True
        elif hit[0].type == "flag" and not hit[0].passed:
            points += 10
            obstacles.remove (hit[0])


    score_text = font.render("Score: " +str(points), 1, (0, 0, 0))
    animate()

2 个回答

1

这可能是因为Windows和Unix系统的换行符问题。你是不是在两个不同的操作系统之间交换Python代码?如果是的话,可以试着运行dos2unix或者unix2dos,这样可以把换行符调整成当前系统IDLE所需要的格式。

5
with open('problem_script.py','r') as f:
    content = f.read()
    print(repr(content))

很可能他的文件里有一些看不见的隐藏字符。使用上面的程序可以帮助他找到这些字符。

撰写回答