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

In summary, the conversation discusses a program for creating a two-dimensional array and the need for help in altering the program. The code involves taking input for student names and grades, and then printing them out with an average grade. The issue of printing the grades without brackets is addressed, and a possible solution is provided.
  • #1
acurate
17
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
  • #2
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:

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

1. How do I convert a two-dimensional array print into a normal format?

To convert a two-dimensional array print into a normal format, you can use a nested for loop to iterate through the rows and columns of the array. As you iterate, you can use the array indices to access and print out the individual elements in a normal format.

2. Can I use a built-in function to change a two-dimensional array print into normal?

Yes, most programming languages have built-in functions that can help you convert a two-dimensional array print into a normal format. These functions are often named something like "flatten" or "reshape". Be sure to check your programming language's documentation for the specific function and its usage.

3. What is the difference between a two-dimensional array print and a normal format?

A two-dimensional array print is a representation of an array where the elements are displayed in a grid-like structure with rows and columns. On the other hand, a normal format simply prints out the elements in a linear sequence. This is often easier to read and understand for humans.

4. Can I change the dimensions of a two-dimensional array when converting it to a normal format?

Yes, you can change the dimensions of a two-dimensional array when converting it to a normal format. This can be useful if you want to flatten a two-dimensional array into a one-dimensional array or vice versa. However, keep in mind that changing the dimensions will also affect the number of elements in the array.

5. What are some common mistakes when converting a two-dimensional array print into normal?

Some common mistakes when converting a two-dimensional array print into normal include not properly iterating through all the rows and columns, accessing the elements using incorrect indices, or not considering the dimensions of the array. It is important to carefully plan and test your code to avoid these mistakes.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
12
Views
3K
  • Programming and Computer Science
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
21
Views
2K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
17
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
992
  • Programming and Computer Science
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
Back
Top