r/pythonhelp • • Dec 09 '22

Problem with understanding lists/arrays and creating a list from another list...

Hi this semester I enrolled in a computer science class and I am confused on this code. I was assigned to create a code that can create a 3x3 multidimensional list that takes the input of elements from the user. I then need to store all the "A" grades in a new list. I attempted to do so and keep getting an error stating "TypeError: '<=' not supported between instances of 'list' and 'int'. Just need help understanding what I am doing wrong. Thanks in advance. Here's my code for reference

R = int(input("Enter the number of rows:"))
C = int(input("Enter the number of columns:"))
matrix = []
print("Enter the entries rowwise:")
for i in range(R): # A for loop for row entries
a =[]
for j in range(C): # A for loop for column entries
a.append(int(input()))
matrix.append(a)
print (matrix)
A_grades=[]
B_Grades=[]
C_Grades=[]

def grades_a(matrix):
for x in matrix:
if x <= 100 and x >= 90:
A_grades.append()
print(A_grades)

grades_a(matrix)

2 Upvotes

3 comments sorted by

1

u/Goobyalus Dec 09 '22

In the future, please format your code properly for Reddit. This is my guess as to how your code is formatted:

R = int(input("Enter the number of rows:"))
C = int(input("Enter the number of columns:"))
matrix = []
print("Enter the entries rowwise:")
for i in range(R): # A for loop for row entries
    a =[]
    for j in range(C): # A for loop for column entries
        a.append(int(input()))
    matrix.append(a)
print (matrix)
A_grades=[]
B_Grades=[]
C_Grades=[]
def grades_a(matrix):
    for x in matrix:
        if x <= 100 and x >= 90:
            A_grades.append()
print(A_grades)
grades_a(matrix)

Is that right?

When you run your code and pring out the matrix, you see something like this, right?

[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

The matrix is a list of lists. So matrix[0] is the sublist [1, 2, 3], and matrix[0][1] is like [1, 2, 3] indexed at 1, so 2.

Does that help you understand the error?:

    if x <= 100 and x >= 90:
       ^^^^^^^^
TypeError: '<=' not supported between instances of 'list' and 'int'

Also have alook at

            A_grades.append()

1

u/Maleficent-Break1738 Dec 09 '22

Yes this helped alot and sorry for the wrong formatting. I understand that I am calling x which is creating a nested list. So I have to make a new loop for that

1

u/Goobyalus Dec 09 '22

Yep.

Just to clarify some of the terminology, because I'm not sure what you mean by calling x. When we write

for x in matrix:

this means to iterate through each element in matrix, assigning the element to the name x successively. So x is assigned matrix[0] in the first iteration, then matrix[1], and so on.

Unless you mean "calling" like "calling the sublist x," calling refers to calling a function. When you do grades_a(matrix), that's "calling" the function grades_a with the argument matrix. We don't "call" a list in that sense.