Search code examples
python-3.xintnested-loops

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?


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

As you can see,im having a problem from getting the average as the total only takes the final test marks and not the total


Solution

  • You need to store the marks:

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

    some small Improvements

    don't increment the value while iterating over it

    i=i + 1
    

    instead add 1 to all outputs or change the range to iterate over


    remove the unnecessary while loop

    while tests>0:
        break
    

    a for-loop for 0 values ends itself automatically