how do i make an array of arrays using for loops to input elements to the array

34 views Asked by At
grades=[]
ClassGrade=[]

NumStudents=int(input(" How Many Students Do You Have ? "))

for i in range(0,2,1):
    prompt=("Input student "+str(i)+"'s grades ")
    for i in range(0,2,1):
        grade=float(input(prompt))
        grades.append(grade)
    ClassGrade.append(grades)


print(ClassGrade)

when start inputting the elements for example student 1 : [56,80] and student 2:[90,66]

and i append grades to classGrade when i print out classGrade i get [[56,80,90,66],[56,80,90,66]]

when i'm actually expecting classGrade to be [[56,80],[90,66]]

1

There are 1 answers

0
Flavio Adamo On

You should declare the array inside the for loop, or you will add all the grades to the same list:

you need to move grades=[] from here:

grades=[] <---
ClassGrade=[]

to:

for i in range(0,2,1):
    grades=[] <---
    prompt=("Input student "+str(i)+"'s grades ")