Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.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 3.x 我不熟悉编码,如果我的问题很愚蠢,请原谅,但我可以知道如何找到每个学生的考试总分吗?_Python 3.x_Int_Nested Loops - Fatal编程技术网

Python 3.x 我不熟悉编码,如果我的问题很愚蠢,请原谅,但我可以知道如何找到每个学生的考试总分吗?

Python 3.x 我不熟悉编码,如果我的问题很愚蠢,请原谅,但我可以知道如何找到每个学生的考试总分吗?,python-3.x,int,nested-loops,Python 3.x,Int,Nested Loops,正如你所看到的,我在获取平均值时遇到了一个问题,因为总数只取最后的测试分数,而不取总数你需要存储分数: students=int(input('How many students do you have?')) tests=int(input('How many test for your module?')) for i in range(students): i=i + 1 print('******* Student # {:0d} *******'.format(i))

正如你所看到的,我在获取平均值时遇到了一个问题,因为总数只取最后的测试分数,而不取总数

你需要存储分数:

students=int(input('How many students do you have?'))
tests=int(input('How many test for your module?'))
for i in range(students):
    i=i + 1
    print('******* Student # {:0d} *******'.format(i))
    while tests>0:
        for t in range(tests):
            t=t+1
            total=int(input('test number {:0d}:'.format(t)))
        break
    average = total/tests
    print('the average for student # {:0.0f} is {:0.1f}'.format(i,average))

一些小的改进

迭代时不要增加值

total = 0
for t in range(tests):
    # note the += instead of the =
    # this adds the new mark instead of overwriting the old one
    total += int(input('test number {:0d}:'.format(t+1)))
而是将1添加到所有输出或更改范围以迭代


删除不必要的while循环

i=i + 1
0值的for循环自动结束

while tests>0:
    break