我是编码新手,如果我的问题很笨,请原谅我,但我可以知道如何找到每个学生的测试总分?

问题描述 投票:0回答:1
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))

正如你所看到的,我有一个问题,从得到的平均,因为总的只需要最后的测试分数,而不是总的分数

python-3.x int nested-loops
1个回答
0
投票

你需要存储的标志。

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)))

一些小小的改进

不要边迭代边递增数值。

i=i + 1

而不是在所有的输出上加1,或者改变范围来迭代处理。


去掉不必要的while循环

while tests>0:
    break

0值的for-loop自动结束。

© www.soinside.com 2019 - 2024. All rights reserved.