Вы используете is
, где вы имеете в виду ==
; нет гарантии, что переменная theClass
и литерал 412, например, оба ссылаются на один и тот же объект , представляющий 412.
>>> theClass = 412
>>> theClass is 412
False
>>> theClass == 412
True
Тот факт, что это верно для 213
>>> theClass = 213
>>> theClass is 213
True
является следствием определенной оптимизации, используемой реализацией CPython (и, возможно, другими) для малых целых чисел, а не языковой гарантией.
def approveSelectedCourse(self, finishedCourses, theClass):
if theClass in finishedCourses:
return False
elif theClass == 213:
if 110 in finishedCourses:
return True
else:
return False
elif theClass == 412:
if 316 in finishedCourses:
return True
else:
return False
, который можно упростить до
def approveSelectedCourse(self, finishedCourses, theClass):
if theClass in finishedCourses:
return False
elif theClass == 213:
return 110 in finishedCourses
elif theClass == 412:
return 316 in finishedCourses