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

Click For Summary
SUMMARY

This discussion focuses on modifying a Python program that manages a two-dimensional array of student names and their corresponding grades. The original implementation printed grades in a list format (e.g., GRADE: [8, 7, 9]), which was not desired. The solution involves restructuring the grades storage to be a list of lists, allowing for individual grade entries per student. The corrected code ensures that grades are printed without brackets, achieving the desired output format.

PREREQUISITES
  • Understanding of Python programming and syntax
  • Familiarity with lists and list operations in Python
  • Basic knowledge of input handling and loops in Python
  • Concept of average calculation in programming
NEXT STEPS
  • Implement a list of lists structure for grades in Python
  • Learn about Python's string manipulation methods for formatting output
  • Explore error handling techniques in Python to improve user input validation
  • Study Python's built-in functions for calculating averages and other statistics
USEFUL FOR

Python developers, educators, and students looking to enhance their skills in managing data structures and improving output formatting in console applications.

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

Replies
7
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 17 ·
Replies
17
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
1
Views
1K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K