0

Can anyone tell me what's wrong with my class code? When I executed it, the program just pop up "Indentation Error: unindent does not match any outer indentation level"

The following is my code:

class Student:

    totalStudents = 0

    def __init__(self, name, year):

        self.name = name

        self.year = 0

        self.grade = []

        self.attend = 0

        print("Add {0} to the classroom".format(self.name) )

        Student.totalStudents += 1

    def addGrade(self, grade):

        self.grade.append(grade)

    def attendDay(self):

        self.attend += 1

    def classAverage(self, grade):

        return sum(self.grade) / len(self.grade)

    def __str__(self, name, grade):

        return "{0} is a {1} grader studnet".format(self.name, self.year)
KillianDS
  • 16,936
  • 4
  • 61
  • 70

2 Answers2

2

While programming with Python you should take care of ;

  • Don't use TAB character

or

  • Be sure your editor to converts your TAB character to space characters
myildirim
  • 2,248
  • 2
  • 19
  • 25
2

This code works when I run it in the current edit of the question -- the question was edited by @KillianDS, and I presume he/she unwittingly fixed the problem by fixing the formatting.

Looking at the original edit, the problem appears to be on your first line after class Student:. The line totalStudents = 0 is indented 2 levels in, whereas the line before it (class Student:) is not indented at all. EDIT: You also mix tabs and spaces, which causes problems.

Your formatting should look like this:

(Note: use 4 spaces, not a tab character! You should adjust your text editor so that it uses spaces, not tabs, when you hit the tab key.)

class Student:
    totalStudents = 0

    def __init__(self, name, year):
        self.name = name
        self.year = 0
        self.grade = []
        self.attend = 0
        print("Add {0} to the classroom".format(self.name) )
        Student.totalStudents += 1
Dave Yarwood
  • 2,866
  • 1
  • 17
  • 29
  • I did not fix it unwittingly, markdown hides the tab/spaces issue (also with the original post), I just added an additional indentation. – KillianDS Jun 26 '14 at 14:03
  • In the original edit, `totalStudents = 0` was indented twice instead of once. That would cause an IndentationError too. – Dave Yarwood Jun 26 '14 at 14:04
  • No, again, it's how markdown displays it, both use a single tab for that indentation. – KillianDS Jun 26 '14 at 14:06
  • Ahh, I see what you're saying now. You just indented the first line, `class Student:` so that it would end up in the code block. I'm not so sure that OP realized he/she needs to put 4 spaces before every line in order to post it to StackOverflow, so I'm assuming that what he/she posted (without an extra 4 spaces on every line) is literally his/her code. – Dave Yarwood Jun 26 '14 at 14:06