2

I have this program:

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")

I need to make the [] disappear between numbers when the program prints them, for example.

When you enter numbers, like this:

Enter a name: Jack
Enter a grade: 8
Enter a name: Jack
Enter a grade: 9
Enter a name: Jack
Enter a grade: 7
Enter a name: 0
A zero is entered.

It prints like this:

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

But I need the program to print like this:

 NAME:  Jack
 GRADE:  8, 9, 7
 AVERAGE:  8.0 

The grades should be without brackets. I think I need to use string or something, does anyone know how?

3 Answers 3

5

strip allows you to remove the specified characters from the beginning and from the end.

str(grades[i]).strip('[]')
1

First:

>>> grades = [[1,2,3],[4,5,6]]

Now you have a few choices:

>>> print("GRADE:", *grades[0])
GRADE: 1 2 3

or:

>>> print("GRADE:", ', '.join(map(str, grades[0])))
GRADE: 1, 2, 3

or, in a script or block:

print("GRADE: ", end='')
print(*grades[0], sep=', ')

result of above:

GRADE: 1, 2, 3

Replace [0] with [i] as needed.

1

If grades is a list of lists of integers:

print(', '.join(str(i) for i in grades[g])) # where g sublist index
1
  • 2
    grades is a list of lists so you will need to use (str(g) for g in grades[i])
    – AChampion
    Commented Apr 8, 2015 at 21:55

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.