user20669683
user20669683

Reputation:

Why am I getting an Attribute Error for my code when it should be working

I have a class ScrollingCredits. In that, I have a method load_credits. Please have a look at the code

class ScrollingCredits:
    def __init__(self):


        self.load_credits("end_credits.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

The first line after defining the function is where my attribute problem occurs I get this brought up when I try to run it: AttributeError: 'ScrollingCredits' object has no attribute 'load_credits'

If anyone would be able to help me it would be much appreciated

Upvotes: 0

Views: 31

Answers (1)

Usman Arshad
Usman Arshad

Reputation: 552

There is function definition and calling issue for load_credits, if you want to access the function with self Make the load_credits outside the __init__ function like below.

class ScrollingCredits:
    def __init__(self):
        self.load_credits("end_credits.txt")
............

    def load_credits(self, filename):
............

Upvotes: 1

Related Questions