创建总和列表

时间:2015-05-01 15:11:35

标签: list python-3.x indexoutofrangeexception

我是Python的新手,我正在努力创建一个由for循环生成的总和列表。

我得到了一份学校作业,我的课程必须在多项选择考试中模拟一类盲人学生的分数。

def blindwalk():       # Generates the blind answers in a test with 21 questions
    import random
    resp = []
    gab = ["a","b","c","d"]
    for n in range(0,21):
        resp.append(random.choice(gab))
    return(resp)

def gabarite():        # Generates the official answer key of the tests
    import random
    answ_gab = []
    gab = ["a","b","c","d"]
    for n in range(0,21):
        answ_gab.append(random.choice(gab))
    return(answ_gab)

def class_tests(A):    # A is the number of students
    alumni = []
    A = int(A)
    for a in range(0,A):
        alumni.append(blindwalk())
    return alumni

def class_total(A):    # A is the number of students
    A = int(A)
    official_gab = gabarite()
    tests = class_tests(A)
    total_score = []*0
    for a in range(0,A):
        for n in range(0,21):
            if  tests[a][n] == official_gab[n]:
                total_score[a].add(1)
    return total_score

当我运行class_total()函数时,我收到此错误:

    total_score[a].add(1)

IndexError: list index out of range

问题是:我如何评估每个学生的分数并用它们创建一个列表,因为这是我想用class_total()函数做的。

我也试过

if  tests[a][n] == official_gab[n]:
                    total_score[a] += 1

但是我得到了同样的错误,所以我认为我还没有完全理解列表在Python中是如何工作的。

谢谢!

(另外,我不是英语母语人士,所以请告诉我,如果我不够清楚的话)

0 个答案:

没有答案