[英]What's wrong with my python classes?
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"
当我执行它时,程序会弹出“缩进错误:unindent与任何外部缩进级别都不匹配”
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)
While programming with Python you should take care of ; 使用Python进行编程时,应注意;
or 要么
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. 当我在问题的当前编辑中运行该代码时,该代码有效-该问题由@KillianDS编辑,并且我认为他/她不经意间通过修复了格式来解决了问题。
Looking at the original edit, the problem appears to be on your first line after class Student:
. 查看原始编辑内容,问题似乎出在
class Student:
后的第一行。 The line totalStudents = 0
is indented 2 levels in, whereas the line before it ( class Student:
) is not indented at all. totalStudents = 0
2个级别缩进,而前面的行( class Student:
)根本不缩进。 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.) (注意:使用4个空格,而不是制表符!您应该调整文本编辑器,以便在按Tab键时使用空格而不是制表符。)
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.