【问题标题】:Im new to coding and pardon me if my question is dumb but may i know how do I find the total marks for the test for each student?我是编码新手,如果我的问题很愚蠢,请原谅我,但我可以知道如何找到每个学生的考试总分吗?
【发布时间】:2020-05-02 14:17:54
【问题描述】:
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】:

    您需要存储标记:

    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 循环自动结束

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-09-10
      • 1970-01-01
      • 2021-05-25
      • 2022-11-03
      • 1970-01-01
      • 1970-01-01
      • 2022-12-14
      相关资源
      最近更新 更多