Python:缩进错误

发布于 2024-11-30 09:20:46 字数 5097 浏览 2 评论 0原文

我的表弟不知何故找到了一本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()

My younger cousin somehow got hold of a python book and has been trying to learn Python. He's asked me for help. But I'm having trouble doing that.

He is using IDLE and he reports that he gets this error:

               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.

He sent me a copy of his code, and the indentation is correct in it. It fails to run because of a number of syntax errors, but that's not important. He reports that he used the Format->Untabify Region but the problem was not fixed.

I can't for the life of me figure out why I can run his python file, but he can't. Does anybody have any idea what is going on with that? I'm sadly currently located a five hour plane flight away else I'd physically see what was going on.

His code is here, it doesn't have any indentation errors when I try to run it, so I doubt it tell you anything helpful. If there is a problem its been somehow removed by the time I got it.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

守望孤独 2024-12-07 09:20:46
with open('problem_script.py','r') as f:
    content = f.read()
    print(repr(content))

他的档案中很可能隐藏着隐形字符。使用上面的程序将帮助他找到它们。

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

Chances are good there are hidden invisible characters in his file. Using the program above will help him find them.

ま柒月 2024-12-07 09:20:46

也许这是 Windows/Unix 行结束问题。您是否在两个不同的操作系统之间交换 python 代码?如果是这样,请尝试运行 dos2unix 或 unix2dos 将行结尾设置为 IDLE 正在运行的系统上的正确格式。

Perhaps this is a Windows/Unix line ending issue. Are you exchanging the python code between two different operating systems? If so try running dos2unix or unix2dos to set the line endings to the proper format on the system IDLE is running.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文