Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Python-setup列表存储30个学生的姓氏以及test1和test2的测试分数,这两个分数都是50分_Python - Fatal编程技术网

使用Python-setup列表存储30个学生的姓氏以及test1和test2的测试分数,这两个分数都是50分

使用Python-setup列表存储30个学生的姓氏以及test1和test2的测试分数,这两个分数都是50分,python,Python,我试图帮助我的儿子(我们都是Python新手)做一些Python作业,他刚刚开始编写代码,我似乎也是。问题如下: 设置一维数组/列表以存储以下内容: 30个学生姓氏 测试1的学生分数-50分 测试2的学生分数-50分 每个学生的总分 输入学生在测试1和测试2中的分数并打分。所有标记必须在输入时验证,无效标记将被拒绝 我们可以为学生的姓氏创建一个列表,但在创建另外两个列表时,不确定如何从那里开始,这两个列表都有测试1和2的分数,需要链接到特定的姓氏。他们还没有学会字典,在我的研究中,我发现这可能是

我试图帮助我的儿子(我们都是Python新手)做一些Python作业,他刚刚开始编写代码,我似乎也是。问题如下:

设置一维数组/列表以存储以下内容:

30个学生姓氏

测试1的学生分数-50分

测试2的学生分数-50分

每个学生的总分

输入学生在测试1和测试2中的分数并打分。所有标记必须在输入时验证,无效标记将被拒绝

我们可以为学生的姓氏创建一个列表,但在创建另外两个列表时,不确定如何从那里开始,这两个列表都有测试1和2的分数,需要链接到特定的姓氏。他们还没有学会字典,在我的研究中,我发现这可能是一种选择,但不能用来解决这个问题

任何帮助都将不胜感激

使用字典:

people = {}
for _ in range(30):
    surname = input('surname: ')
    ok = False
    while not ok:
        try:
            test_1 = int(input('test 1 score: '))
            test_2 = int(input('test 2 score: '))
            if not (0 <= test_1 <= 50 and 0 <= test_2 <= 50): raise ValueError
            ok = True
        except ValueError:
            print('please enter integers between 0 and 50')
    people[surname] = {'test 1': test_1, 'test 2': test_2, 'total': test_1 + test_2}

欢迎来到stackoverflow。请包括一些最低限度的代码,并向我们展示您的尝试。有关指导,请查看页面和。您是说您需要在一个1d列表中获得所有四个值(姓氏、test1、test2、总分)?还是4个单独的1d列表?此外,什么构成了标记的验证程序?“输入”是如何接收的?确切地说是使用来自键盘的原始输入,还是有一个数据列表可以用作起始值?谢谢您的回复。OP中的信息是我们唯一可以使用的。我们还没有收到起始值的数据列表,因此将使用随机数据测试程序是否正常工作。我对这个问题的理解是,我们需要输入30个学生姓氏的名字以及测试1和2的值。将使用测试1和测试2输入的值计算总分。我可以创建列表来获取姓氏和考试分数,但如何将考试分数与相关学生联系起来?如果你不能使用字典或多维列表,你只需要跟踪给定学生的索引,并确保学生的分数与其他列表中的索引相同。然后使用下面的@Joe Iddon第二种方法。谢谢你,非常感谢你的帮助。“去用它,看看我们能得到什么。”@andrew_reece my bad,正在测试它们,谢谢你的帮助@JoeIddon,非常感谢。向上投票,但我的声誉很低,无法注册。@Ninjahugs您可以接受答案,单击灰色勾号(它将变为绿色)显示这一点很有帮助
people['charles']['test 1']
people = {}
for _ in range(30):
    surname = input('surname: ')
    ok = False
    while not ok:
        try:
            test_1 = int(input('test 1 score: '))
            test_2 = int(input('test 2 score: '))
            if not 0 < test_1 < 50 and 0 < test_2 < 50: raise ValueError
            ok = True
        except ValueError:
            print('please enter integers between 0 and 50')
    people[surname] = {'test 1': test_1, 'test 2': test_2, 'total': test_1 + test_2}
people = []
test_1_scores = []
test_2_scores = []
total_scores = []
for _ in range(30):
    surname = input('surname: ')
    ok = False
    while not ok:
        try:
            test_1 = int(input('test 1 score: '))
            test_2 = int(input('test 2 score: '))
            if not (0 <= test_1 <= 50 and 0 <= test_2 <= 50): raise ValueError
            ok = True
        except ValueError:
            print('please enter integers between 0 and 50')
    people.append(surname)
    test_1_scores.append(test_1)
    test_2_scores.append(test_2)
    total_scores.append(test_1 + test_2)
test_1_scores[people.index('charles')]