简体   繁体   中英

can't print first row or column of names in a 2D matrix in python without numpy. Will only print the float numbers in the matrix

I'm trying to print a 2D matrix in python. However, I can only print the float test scores and not the strings the correspond with the row and column headings.

I'm trying to print this:

studentName ex1 ex2 ex3
mike 78.0 89.0 89.0
Sarah 98.0 78.0 65.0
David 84.0 83.0 98.0

But only get the float numbers and can't figure out how to add the row and column headings.

THIS IS WHAT I HAVE THUS FAR:

studentName = int(input("enter the number of student: "))
studentExam = int(input("how many exam scores: "))
names_students = []
# Initialize matrix
matrix = []
# For user input
for i in range(studentName):  # A for loop for row entries
    exam_student = []
    names_students.append(input("enter name of students" + str(i+1) + ": "))
    for j in range(studentExam):  # A for loop for column entries
        exam_student.append(float(input("enter exam " + str(j+1) + ": ")))
    matrix.append(exam_student)

#for printing
for i in range(studentName):
    for j in range(studentExam):
        print(matrix [i][j], end=" ")
    print()

A little modification in the code is needed, although the code is not efficient in complexity

studentName = int(input("enter the number of student: "))
studentExam = int(input("how many exam scores: "))
columns = ['name','s1','s2','s3']
# Initialize matrix
matrix = [columns]
# For user input
for i in range(studentName):  # A for loop for row entries
    exam_student = []
    exam_student.append(input("enter name of students" + str(i+1) + ": "))
    for j in range(studentExam):  # A for loop for column entries
        exam_student.append(float(input("enter exam " + str(j+1) + ": ")))
    matrix.append(exam_student)

#for printing
for i in range(len(matrix)):
    for j in range(len(matrix)):
        print(matrix [i][j], end=" ")
    print()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM