如何处理列表索引超出范围?

时间:2016-07-19 03:56:54

标签: python

class student:
    def __init__(self,name,course,course_hastaken):
        self.name=name
        self.course=course
        self.taken=course_hastaken

    def takeclass(self):        
        for j in range(len(Course_of_study)):
            if people[i].course==Course_of_study[j]:
                print(Course_of_study[j])
                if Degree_requirement[j] == []:
                    print(people[i].name + " You can take this course")
                else:
                    Requirement =Degree_requirement[j]
                    #print(Requirement)
                    for k in range(len(people[i].taken)):
                        q=people[i].taken
                        for l in range(len(Degree_requirement[j])):
                            p=Degree_requirement[j]
                            if q[k]==p[l]:
                                print(Requirement)
                                print(p[l])
                                Requirement.remove(p[l])
                    if Requirement == []:
                        print("You can take this course")
                    else:
                        print(people[i].name+" Sorry, you can't take this class.")


Course_of_study=["a","b","c","d","e","f","g","h","i"]
Degree_requirement=[["b","g"],[],["d","h","i"],[],["f","b","i"],[],[],[],[]]
people=[student("Jake","a",["b","g","c"]),student("Leo","b",["d","e"]),student("Eric","c",["d"])]
for i in range(len(people)):
    people[i].takeclass()

我的问题是,我发现必需实际上有q [l],但它仍然显示列表超出范围。我打印出p [l]并且需求在列表中有,但是当我输入删除时,程序就出错了。

程序显示

有问题
Requirement=Requirement.remove(p[l])

0 个答案:

没有答案