Reputation: 1
I need a two dimensional list to store information about students and their grades.
When I run my program I just get one list of the numbers but I need different lists for every student.
This is what I've done so far:
COLS= int(input("number of students to enter "))
ROWS= int(input("number of grades per student "))
def main():
number =[]
for c in range(COLS):
grades = []
student =(input("enter student ID number "))
number.append(student)
number.append(grades)
for r in range (ROWS):
grade =(input("Enter grade for module: "))
grades.append(grade)
print(number)
When I run the program it says that there is an indented block and highlight "grades" What am I doing wrong?
Upvotes: 0
Views: 651
Reputation: 47
Two dimensional lists look like this:
two_dim = [[0,1,1,1,1,1],
[1,0,1,0,0,0],
[1,1,0,1,1,0]]
So the rows are each different lists and they are combined in another list. To call an element, you just need to enter
two_dim[row_number][column_number]
That was the general answer for Google searchers, now for your problem, you need to wrap your input in brackets to pass the rows as lists and not end up with a single dimensional list. I would use something like this instead:
student_count= int(input("number of students to enter "))
grade_count = int(input("number of grades per student "))
grades = []
for i in range(student_count):
print("The next student")
grades.append([int(input("The next student grade")) for j in range(grade_count)])
print(grades)
Upvotes: 0
Reputation: 159
Matrix = [5][5]
Matrix = [[0 for x in range(5)] for x in range(5)]
You can now add items to the list:
Matrix[0][0] = 1
Matrix[4][0] = 5
print Matrix[0][0] # prints 1
print Matrix[4][0] # prints 5
Upvotes: 0
Reputation: 193
What you probably want is to have a list that contains the student ID in the first position and grades in subsequent positions:
students= int(input("number of students to enter: "))
grades= int(input("number of grades per student: ")) + 1
class = []
for i in range(students):
for j in range(grades):
if j == 0:
m = input("Please input Student ID: ")
class.append(m)
elif:
m = input("Please input %j th grade: ")
class.append(m)
Upvotes: 1
Reputation: 239000
The code with correct indentation:
COLS= int(input("number of students to enter "))
ROWS= int(input("number of grades per student "))
def main():
number =[]
for c in range(COLS):
grades = []
student =(input("enter student ID number "))
number.append(student)
number.append(grades)
for r in range (ROWS):
grade =(input("Enter grade for module: "))
grades.append(grade)
print(number)
main()
Now it executes.
Here is alternative version, better IMO:
from collections import OrderedDict
COLS= int(input("number of students to enter: "))
ROWS= int(input("number of grades per student: "))
def main():
student_grades = OrderedDict()
for c in range(COLS):
student =(input("enter student ID number: "))
student_grades[student] = []
for r in range(ROWS):
grade =(input("Enter grade for module: "))
student_grades[student].append(grade)
print(student_grades)
# exaple output OrderedDict([('123', ['2', '3']), ('412', ['4', '5'])])
main()
Upvotes: 1