class courseInfo(object):
def __init__(self, courseName):
self.courseName = courseName
self.grade = "No Grade"
def setGrade(self, grade):
if self.grade == "No Grade":
self.grade = grade
def getGrade(self):
return self.grade
class edx(object):
def __init__(self, courses):
self.myCourses = []
for course in courses:
self.myCourses.append(courseInfo(course))
def setGrade(self, grade, course="6.01x"):
"""
grade:integer greater than or equal to 0 and less than or equal to 100
course: string
This method sets the grade in the courseInfo object named by `course`.
If `course` was not part of the initialization, then no grade is set, and no
error is thrown.
The method does not return a value.
"""
for crs in self.myCourses:
if crs.courseName == course:
crs.setGrade(grade)
def getGrade(self, course="6.02x"):
"""
course: string
This method gets the grade in the the courseInfo object named by `course`.
returns: the integer grade for `course`.
If `course` was not part of the initialization, returns -1.
"""
for crs in self.myCourses:
if crs.courseName == course:
return crs.getGrade()
else:
return -1
我对上述代码的测试用例是:
> edX = edx( ["6.00x","6.01x","6.02x"] )
> edX.setGrade(100, "6.00x")
> edX.getGrade("6.00x")
> 100
到目前为止还不错,但是运行edX.setGrade(50, "6.00x")
并不需要,而且getGrade
仍然返回100
。而且,为6.01
和6.02
设置等级似乎不起作用,而且getGrade
总是返回-1
。你知道吗
任何帮助指点将不胜感激!你知道吗
(完全披露:这是一个在线课程。但我不认为这里有任何破坏者,我真的想了解发生了什么。(Tks)
当然,它只工作一次,你这样编码:
设置等级后,测试
self.grade == "No Grade"
不再为真。你知道吗在
getGrade()
方法中确实存在问题:如果第一个课程与名称不匹配,则返回
-1
;从for
循环返回。也许您只想在测试完所有课程后返回-1
:现在
for
循环只有在找到匹配的进程时才会中断,并且除了"6.00x"
之外,您不再返回-1
。你知道吗相关问题 更多 >
编程相关推荐