How to change a two-dimensional array print into normal?

AI Thread Summary
The discussion revolves around modifying a two-dimensional array program to print grades without brackets. The initial code structure leads to grades being stored as a single list, causing the output to display in brackets. A suggested solution involves creating a list of lists for grades, ensuring each student's grades are stored separately. The revised code allows for individual grade entries and corrects the print format to display grades without brackets. The main focus is on restructuring the grades data structure to achieve the desired output format.
acurate
Messages
17
Reaction score
1

Homework Statement


I made a two-dimensional array program, but now I need some help to alter the program in the print zone.

The Attempt at a Solution


Code:
    import sys
    students = []
    grades = []
   
    while True:
        student = input ("Enter a name: ").replace(" ","")
        if  student.isalpha() == True and student != "0":
            while True:
                grade = input("Enter a grade: ").replace(" ","")
                if grade == "0" or grade == 0:
                    print ("\n")
                    print ("A zero is entered.")
                    sys.exit(0)
                if grade.isdigit()== True: 
                    grade = int(grade)
                    if grade >= 1 and grade <= 10:
                        if student in students:
                            index = students.index(student)
                            grades[index].append(grade)
                            break
                        else:
                            students.append(student)
                            grades.append([grade])
                            break
                    else:
                        print("Invalid grade.")
        elif student == "0": 
            print("A zero is entered.")
            break
        else:
            print ("Invalid name.")
    for i in range(0,len(students)): 
        print("NAME: ", students[i])
        print("GRADE: ", grades[i])
        print("AVERAGE: ", round(sum(grades[i])/len(grades[i]),1), "\n")

It prints out like this:

Code:
    NAME:  Jack
    GRADE:  [8, 7, 9]
    AVERAGE:  8.0

How do I turn the
Code:
GRADE:  [8, 7, 9]
so that the numbers would go
Code:
GRADE: 8, 7, 9
?[/B]
 
Physics news on Phys.org
The short answer is, you can't print the grades without the brackets. The reason for this is how your program is working. For a given student, your grades list contains only one element -- a list of the grades that you enter using this line:
Code:
grade = input("Enter a grade: ").replace(" ","")
When you append grade to your grades list, this list looks like so in memory:
grades[0] -- [8, 7, 9]
grades[1] -- empty
grades[2] -- empty
etc.
The fix for this is that after you input each grade (not all of them at once), you append that grade to your list.

Here's some code that I wrote that fixes the problem. It works correctly for a single student, but it doesn't work correctly if you enter more than one student, since grades is only one list -- IOW, there are not separate grades lists for each student. You might consider making grades a list of lists, where the list at index 1 contains the grades for student 1, and the list at index 2 contains the grades for student 2, and so on.
Python:
import sys
students = []
grades = []
sum_grades = 0

while True:
    student = input ("Enter a name: ").replace(" ","")
    if  student.isalpha() == True and student != "0":
        students.append(student)
        while True:
            grade = input("Enter a grade: ")
            if grade == "0" or grade == 0:
                print ("\n")
                print ("A zero is entered.")
                break
            if grade.isdigit()== True:
                grade = int(grade)
                if grade >= 1 and grade <= 10:
                    grades.append(grade)
                else:
                    print("Invalid grade.")

   else:
       break

students_len = len(students)
grades_len = len(grades)
for i in range(students_len):
    print("NAME: ", students[i])
    print('GRADE:', end=' ')
    for j in range(grades_len):
        grade = grades[j]
        sum_grades = sum_grades + grade
        print(grade, end=' ')
        print("\n")
        print("AVERAGE: ", round(sum_grades/len(grades)), "\n")
 
Last edited:

Similar threads

Back
Top