Click here to Skip to main content
15,921,210 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i want to get the average mark for all quizzes but it show index out of range error. Why? Same calculation method i use to find average marks for student but that works, why?


Python
<pre>

R = int(input("Enter the number of rows:"))
C = int(input("Enter the number of columns:"))


m1 = []
m2 = []
m3 = []
m4 = []

for i in range(R):
    a = []
    for j in range(C):
         a.append(int(input("Student " + str(i+1) + " Quiz " + str(j+1)+":")))
    m1.append(a)
    m2.append(a)
    m3.append(a)
    m4.append(a)

for i in range(R):
    T1 = 0
    for j in range(C):
        T1 += m1[i][j]
    m2[i] = T1/C

for i in range(R):
    print("The average mark for student", i+1, "is", m2[i])

print(m1)
print(m2)
print(m3)
print(m4)



for j in range(C):
    T2 = 0
    for i in range(R):
        T2 += m3[i][j]
    m4[j] = T2/R

for j in range(C):
    print("The average mark for quiz", j+1, "is", float(m4[j]))


What I have tried:

Before that i've tried many times but come up to this solution.
Posted
Updated 28-Nov-20 4:28am
Comments
Richard MacCutchan 28-Nov-20 4:45am    
I just ran your code and it worked fine. You need to collect more information. It would certainly help if your showed the input values you used, and exactly where the error occurred.
Wong Zi Ao 28-Nov-20 9:59am    
Hi, good evening.
Try input 2 to row, and 3 to column, then the error would be shown.
Richard MacCutchan 28-Nov-20 10:28am    
See my solution below.
Wong Zi Ao 28-Nov-20 21:40pm    
hi Richard, thanks for helping me to fix the problem. I got it now. For the naming convention, thank you for helping me to improvise it. I will listen to your advice. Thanks again
Richard MacCutchan 29-Nov-20 4:24am    
You are welcome.

Quote:
i want to get the average mark for all quizzes but it show index out of range error. Why?

Pay attention to m1, how do you make it a 2D array ?

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

27.3. pdb — The Python Debugger — Python 3.6.1 documentation[^]
Debugging in Python | Python Conquers The Universe[^]
pdb – Interactive Debugger - Python Module of the Week[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
The problem is the confusion between rows and columns. A better naming system would use students and quizzes. Also the use of array m4 is not needed, so you can remove it. To get the quiz averages change the final loop to the following:
Python
for j in range(C): // the number of quizzes
    average = 0
    for i in range(R):  // the number of students
        average += m3[i][j]  // calculate the total score for each quiz per student
    print("The average mark for quiz", j+1, "is", round(float(average/R), 2))

Using more meaningful names for your variables helps to make the code easier to understand, especially when it goes wrong.

[edit]
A slightly improved version of the complete program:
Python
students = int(input("Enter the number of students:"))
quizzes = int(input("Enter the number of quizzes:"))

scores = []

for i in range(students):
    a = []
    for j in range(quizzes):
         a.append(int(input("Student " + str(i+1) + " Quiz " + str(j+1)+":")))
    scores.append(a)

print(' ')
for i in range(students):
    average = 0
    for j in range(quizzes):
        average += scores[i][j]
    print("The average mark for student", i+1, "is", average / quizzes)

print(' ')
for j in range(quizzes):
    average = 0
    for i in range(students):
        average += scores[i][j]
    print("The average mark for quiz", j+1, "is", round(float(average / students), 2))

[/edit]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900