python-3.x 终端中的属性错误

wbgh16ku  于 2022-12-05  发布在  Python
关注(0)|答案(1)|浏览(136)

请帮帮忙

class ScrollingCredits:

    def __init__(self):

        self.load_credits('assignment.txt')

        (self.background, self.background_rect) = \
            load_image('starfield.gif', True)

        self.font = pygame.font.Font(None, FONT_SIZE)

        self.scroll_speed = SCROLL_SPEED

        self.scroll_pause = SCROLL_PAUSE

        self.end_wait = END_WAIT

        self.reset()

        def load_credits(self, filename):

            f = open(filename)

            credits = []

            while 1:

                line = f.readline()

                if not line:

                    break

            line = string.rstrip(line)

            credits.append(line)

            f.close()

            self.lines = credits

我收到以下错误

line 66, in __init__
    self.load_credits('assignment.txt')
AttributeError: 'ScrollingCredits' object has no attribute 'load_credits'

我想知道它是否可能是assignmentidertxt,但我不是100%,我谷歌了一下,但我似乎找不到解决方案,帮助将非常感激

eaf3rand

eaf3rand1#

正如Wondercricket的注解所建议的,您应该减少load_credits的缩进。正确的代码是:

class ScrollingCredits:

    def __init__(self):

        self.load_credits('assignment.txt')

        (self.background, self.background_rect) = \
            load_image('starfield.gif', True)

        self.font = pygame.font.Font(None, FONT_SIZE)

        self.scroll_speed = SCROLL_SPEED

        self.scroll_pause = SCROLL_PAUSE

        self.end_wait = END_WAIT

        self.reset()

    def load_credits(self, filename):

        f = open(filename)

        credits = []

        while 1:

            line = f.readline()

            if not line:

                break

        line = string.rstrip(line)

        credits.append(line)

        f.close()

        self.lines = credits

相关问题