为什么这会继续给我一个列表索引超出范围错误?

时间:2016-09-28 23:56:37

标签: python

count = 0
badIndices = [59,64,68,72,74,77,79,103,104,108,109,118,119,123,124,130,133,139]
test1 = []
csvCourses = []
csvExamTime = []
examTime = []

with open("final_exam_schedule.csv") as file:
    reader = csv.reader(file, delimiter = ",")
    next(reader)
    for row in reader:
        csvCourses.append(row)

for i in range(len(csvCourses)):
    csvExamTime.append(csvCourses[i][5])
    examTime.append(findSchedule(csvCourses[i][4],csvCourses[i][1],csvCourses[i][3],csvCourses[i][2]))

for i in badIndices:
    test1.append(csvCourses[i][5])

for i in badIndices:
    if count >= len(test1):
        break
    else:
        count += 1
        examTime[i] = test1[count] #this is where I get my error

每当我尝试将examTime [i]分配给test1时,它会让我“超出范围”,但我认为我的if语句确保它没有超出范围?

作为参考,examTime包含155个不同的索引,badIndices列表是那些没有返回值的索引,所以我将转到那个确切的索引并为其赋值。 findSchedule()是我的函数,有一堆ifs和elses来分配examTime,我没有包含它,因为它真的很长而且不需要问题。只有特定的坏指数不会返回值,其余的则不会。

编辑:ok这里是findSchedule函数

def findSchedule(days,time,cr,period):          
    if days == "TR":
        if time == "11:00":
            if period == "AM":
                return("Tuesday, December 13, 9:45 - 11:45 am")
            elif period == "PM":
                return("Class final not found, please try again")     
        elif time == "12:30" or time == "1:00":
            if period == "PM":
                return("Tuesday, December 13, 12:00 - 2:00 pm")
            elif period == "AM":
                return("Class final not found, please try again")
        elif time == "3:30":
            if period == "PM":
                return("Tuesday, December 13, 2:15 - 4:15 pm")
            elif period == "AM":
                return("Class final not found, please try again")
        elif time == "5:00":
            if period == "PM":
                return("Tuesday, December 13, 4:30 - 6:30 pm")
            elif period == "AM":
                return("Class final not found, please try again")
        elif time == "6:30":
            if period == "PM":
                return("Tuesday, December 13, 7:00 - 9:00 pm")
            elif period == "AM":
                return("Class final not found, please try again")
        elif time == "8:00":
            if period == "AM":
                return("Wednesday, December 14, 7:30 - 9:30 am")
            elif period == "PM":
                return("Class final not found, please try again")
        elif time == "2:00":
            if period == "PM":
                return("Thursday, December 15, 12:00 - 2:00 pm")
            elif period == "AM":
                return("Class final not found, please try again")
        elif time == "9:30":
            if period == "AM":
                return("Friday, December 16, 9:45 - 11:45 am")
        elif time == "9:00":
            if period == "AM":
                return("Friday, December 16, 7:30 - 9:30 am")
        elif time == "10:00":
            if period == "AM":
                return("Wednesday, December 14, 9:45 - 11:45 am")
        elif time == "12:00":
            if period == "PM":
                return("Wednesday, December 14, 12:00 - 2:00 pm")
        elif time == "5:00" and cr == "5":
            return("Wednesday, December 14, 4:30 - 6:30 pm")
    elif days == "MWF":
        if time == "10:00":
            if period == "AM":
                return("Wednesday, December 14, 9:45 - 11:45 am")
            elif period == "PM":
                return("Class final not found, please try again")
        elif time == "12:00":
            if period == "PM":
                return("Wednesday, December 14, 12:00 - 2:00 pm")
            elif period == "AM":
                return("Class final not found, please try again")
        elif time == "3:00":
            if period == "PM":
                return("Wednesday, December 14, 2:15 - 4:15 pm")
            elif period == "AM":
                return("Class final not found, please try again")
        elif time == "5:00":
            if period == "PM":
                return("Wednesday, December 14, 4:30 - 6:30 pm")
            elif period == "AM":
                return("Class final not found, please try again")
        elif time == "6:30" or time == "8:00":
            if period == "PM":
                return("Wednesday, December 14, 7:00 - 9:00 pm")
            elif period == "AM" and time == "8:00":
                return("Thursday, December 15, 7:30 - 9:30 am")
        elif time == "11:00":
            if period == "AM":
                return("Thursday, December 15, 9:45 - 11:45 am")
            elif period == "PM":
                return("Class final not found, please try again")
        elif time == "2:00":
            if period == "PM":
                return("Thursday, December 15, 2:15 - 4:15 pm")
            elif period == "AM":
                return("Class final not found, please try again")
        elif time == "9:00":
            if period == "AM":
                return("Friday, December 16, 7:30 - 9:30 am")
            elif period == "PM":
                return("Class final not found, please try again")
        elif time == "1:00":
            if period == "PM":
                return("Friday, December 16, 12:00 - 2:00 pm")
            elif period == "AM":
                return("Class final not found, please try again")
        elif time == "4:00":
            if period == "PM":
                return("Friday, December 16, 2:15 - 4:15 pm")
            elif period == "AM":
                return("Class final not found, please try again")
        elif time == "8:00":
            if period == "AM":
                return("Thursday, December 15, 7:30 - 9:30 am")
        elif time == "9:30":
            return("Friday, December 16, 9:45 - 11:45 am")
        elif time == "3:30":
            return("Tuesday, December 13, 2:15 - 4:15 pm")
        elif time == "3:00":
            return("Wednesday, December 14, 2:15 - 4:15 pm")
        elif time == "11:00" and cr == "5":
            if period == "AM":
                return("Thursday, December 15, 9:45 - 11:45 am")
    elif days == "MW":
        if time == "8:00":
            if period == "PM":
                return("Wednesday, December 14, 7:00 - 9:00 pm")
            elif period == "AM":
                return("Thursday, December 15, 7:30 - 9:30 am")
        elif time == "11:00":
            if period == "AM":
                return("Thursday, December 15, 9:45 - 11:45 am")
            elif period == "AM":
                return("Class final not found, please try again")
        elif time == "2:00":
            if period == "PM":
                return("Thursday, December 15, 2:15 - 4:15 pm")
            elif period == "AM":
                return("Class final not found, please try again")
        elif time == "1:00":
            if period == "PM":
                return("Friday, December 16, 12:00 - 2:00 pm")
            elif period == "AM":
                return("Class final not found, please try again")
        elif time == "10:00":
            if period == "AM":
                return("Wednesday, December 14, 9:45 - 11:45 am")
        elif time == "9:00":
            return("Friday, December 16, 7:30 - 9:30 am")
        elif time == "9:30":
            return("Friday, December 16, 9:45 - 11:45 am")
        elif time == "9:30":
            return("Friday, December 16, 9:45 - 11:45 am")
        elif time == "3:30":
            return("Tuesday, December 13, 2:15 - 4:15 pm")
        elif time == "6:30" and cr == "1":
            return("Monday, December 12, 6:30 - 7:25 pm")
        elif time == "6:30" and cr == "2":
            return("Monday, December 12, 6:30 - 8:20 pm")
        elif time == "6:30" and cr == "3":
            return("Monday, December 12, 6:30 - 9:30 pm")
        elif time == "6:30" and cr == "4":
            return("Monday, December 12, 6:30 - 10:15 pm")
        elif time == "5:00" and cr == "5":
            if period == "PM":
                return("Wednesday, December 14, 4:30 - 6:30 pm")

    elif days == "R":
        if time == "6:30" and period == "PM":
            return("Thursday, December 15, 7:00 - 9:00 pm")
    elif days == "WF":
        if time == "8:00" and (cr == "2" or cr == "5"):
            if period == "AM":
                return("Thursday, December 15, 7:30 - 9:30 am")
            elif period == "PM":
                return("No class found")
        elif time == "8:00" and cr == "4":
            if period == "AM":
                return("Wednesday, December 14, 7:30 - 9:30 am")
            elif period == "PM":
                return("Class not found")
        elif time == "9:00":
            if period == "AM":
                return("Friday, December 16, 7:30 - 9:30 am")
            if period == "PM":
                return("Class not found")
        elif time == "9:30":
            if period == "AM":
                return("Friday, December 16, 9:45 - 11:45 am")
            elif period == "PM":
                return("Class not found")
        elif time == "10:00":
            if period == "AM":
                return("Wednesday, December 14, 9:45 - 11:45 am")
            elif period == "PM":
                return("Class not found")
        elif time == "11:00":
            if period == "AM":
                return("Thursday, December 15, 9:45 - 11:45 am")
            elif period == "PM":
                return("Class not found")
        elif time == "12:00":
            if period == "PM":
                return("Wednesday, December 14, 12:00 - 2:00 pm")
            elif period == "AM":
                return("Class not found")
        elif time == "12:30":
            if period == "PM":
                return("Tuesday, December 13, 12:00 - 2:00 pm")
            elif period == "AM":
                return("Classs not found")
        elif time == "1:00":
            if period == "PM":
                return("Friday, December 16, 12:00 - 2:00 pm")
            elif period == "AM":
                return("Class not found")
        elif time == "2:00":
            if period == "PM":
                return("Thursday, December 15, 12:00 - 2:00 pm")
            elif period == "AM":
                return("Class not found")
        elif time == "3:00":
            if period == "PM":
                return("Wednesday, December 14, 2:15 - 4:15 pm")
            elif period == "AM":
                return("Class not found")
        elif time == "3:30":
            if period == "PM":
                return("Tuesday, December 13, 2:15 - 4:15 pm")
            elif period == "AM":
                return("Class not found")
        elif time == "4:00":
            if period == "PM":
                return("Friday, December 16, 2:15 - 4:15 pm")
            elif period == "AM":
                return("Class not found")
        elif time == "5:00" and cr == "2":
            if period == "PM":
                return("Tuesday, December 13, 4:30 - 6:30 pm")
            elif period == "AM":
                return("Class not found")
        elif time == "5:00" and cr == "4":
            if period == "PM":
                return("Wednesday, December 14, 4:30 - 6:30 pm")
            elif period == "AM":
                return("Class not found")
        elif time == "9:30":
            return("Friday, December 16, 9:45 - 11:45 am")
        elif time == "3:30":
            return("Tuesday, December 13, 2:15 - 4:15 pm")
    elif days == "M":
        if time == "6:30":
            return("Monday, December 12, 6:30 - 7:25 pm")
    elif days == "T":
        if time == "6:30":
            return("Tuesday, December 13, 7:00 - 9:00 pm")
    else:
        if (cr == "4" or cr == "2") and time == "12:30":
            if period == "PM":
                return("Tuesday, December 13, 1:00 - 2:00 pm")
            elif period == "AM":
                return("Class final not found, please try again")
        elif cr == "4" and time == "3:30":
            if period == "PM":
                return("Tuesday, December 13, 2:15 - 4:15 pm")
            elif period == "AM":
                return("Class final not found, please try again")
        elif cr == "4" and time == "5:00":
            if period == "PM":
                return("Tuesday, December 13, 4:30 - 6:30 pm")
            elif period == "AM":
                return("Class final not found, please try again")
        elif cr == "4" and time == "8:00":
            if period == "AM":
                return("Wednesday, December 14, 7:30 - 9:30 am")
            elif period == "PM":
                return("Class final not found, please try again")
        elif time == "10:00":
            if period == "AM":
                return("Wednesday, December 14, 9:45 - 10:45 am")
            elif period == "PM":
                return("Class final not found, please try again")
        elif time == "12:00":
            if period == "PM":
                return("Wednesday, December 14, 12:00 - 2:00 pm")
            elif period == "AM":
                return("Class final not found, please try again")
        elif time == "3:00":
            if period == "PM":
                return("Wednesday, December 14, 2:15 - 4:15 pm")
            elif period == "AM":
                return("Class final not found, please try again")
        elif time == "5:00":
            if period == "PM":
                return("Wednesday, December 14, 2:15 - 4:15 pm")
            elif period == "AM":
                return("Class final not found, please try again")
        elif cr == "5" and time == "8:00":
            if period == "AM":
                return("Thursday, December 15, 7:30 - 9:30 am")
            elif period == "PM":
                return("Class final not found, please try again")
        elif time == "11:00":
            if period == "AM":
                return("Thursday, December 15, 9:45 - 11:45 am")
            elif period == "PM":
                return("Class final not found, please try again")
        elif cr == "4" and time == "2:00":
            if period == "PM":
                return("Thursday, December 15, 12:00 - 2:00 pm")
            elif period == "AM":
                return("Class final not found, please try again")
        elif cr == "5" and time == "2:00":
            if period == "PM":
                return("Thursday, December 15, 2:15 - 4:15 pm")
            elif period == "AM":
                return("Class final not found, please try again")
        elif time == "9:00":
            if period == "AM":
                return("Friday, December 16, 7:30 - 9:30 am")
            elif period == "PM":
                return("Class final not found, please try again")
        elif time == "9:30":
            if period == "AM":
                return("Friday, December 16, 9:45 - 10:45 am")
            elif period == "PM":
                return("Class final not found, please try again")
        elif time == "1:00":
            if period == "PM":
                return("Friday, December 16, 12:00 - 2:00 pm")
            elif period == "PM":
                return("Class final not found, please try again")
        elif time == "4:00":
            if period == "PM":
                return("Friday, December 16, 2:15 - 4:15 pm")
            elif period == "AM":
                return("Class final not found, please try again")   
        else:
            return("Class final not found, please try again")

1 个答案:

答案 0 :(得分:0)

如果不知道您的数据是什么样子很难说,但这就是我的想法,

您的count变量过早增加。在python列表中,索引从0开始,然后转到len(list)-1作为最后一个索引。您可以从count=1开始,因此在最后一次迭代count=len(list)上可能会出现错误。尝试将其作为代码后半部分的更正:

for i in badIndices:
    if count >= len(test1):
        break
    else:
        examTime[i] = test1[count]
        #We increment count after it is used
        #to avoid an error
        count+=1