1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

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

  1. Mar 27, 2015 #1
    1. The problem statement, all variables and given/known data
    I made a two-dimensional array program, but now I need some help to alter the program in the print zone.

    3. The attempt at a solution
    Code (Text):

        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 (Text):

        NAME:  Jack
        GRADE:  [8, 7, 9]
        AVERAGE:  8.0
     
    How do I turn the
    Code (Text):
    GRADE:  [8, 7, 9]
    so that the numbers would go
    Code (Text):
    GRADE: 8, 7, 9
    ?
     
  2. jcsd
  3. Mar 27, 2015 #2

    Mark44

    Staff: Mentor

    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 (Text):
    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.
    Code (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: Mar 27, 2015
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: How to change a two-dimensional array print into normal?
Loading...