The IT Service Excellence Tool Kit has best practices to keep your clients happy and business booming. Inside, you’ll find everything you need to increase client satisfaction and retention, become more competitive, and increase your overall success.

I have the following assignment:

---

A GPA, or Grade point Average, is calculated by summing the grade points earned in a student’s courses and then dividing by the total units. The grade points for an individual course are calculated by multiplying the units for that course by the appropriate factor depending upon the grade received:

A receives 4 grade points

B receives 3 grade points

C receives 2 grade points

D receives 1 grade point

F receives 0 grade point

Your program will have a for loop to calculate multiple GPAs and a for loop to collect individual grades (i.e. a nested for loop).

---

Any ideas about how to approach this? It seems to me that there has to also be a while loop involved, since we don't know how many courses the user needs to input. Is that true? Is there a way to implement something like a sentinel using only for loops?

---

A GPA, or Grade point Average, is calculated by summing the grade points earned in a student’s courses and then dividing by the total units. The grade points for an individual course are calculated by multiplying the units for that course by the appropriate factor depending upon the grade received:

A receives 4 grade points

B receives 3 grade points

C receives 2 grade points

D receives 1 grade point

F receives 0 grade point

Your program will have a for loop to calculate multiple GPAs and a for loop to collect individual grades (i.e. a nested for loop).

---

Any ideas about how to approach this? It seems to me that there has to also be a while loop involved, since we don't know how many courses the user needs to input. Is that true? Is there a way to implement something like a sentinel using only for loops?

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get this solution by purchasing an Individual license!
Start your 7-day free trial.

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trialI actually ended up going about it in a completely different way (before reading your response). This is what I wrote:

---

todo = int(input("How many GPA's would you like to calculate? "))

for x in range (1, todo+1):

n = int(input("How many courses will you input? "))

totpoints = 0

totunits = 0

for x in range(1, n+1):

grade = input("Enter grade for course: " )

if grade == 'A':

grade = int(4)

if grade == 'B':

grade = int(3)

if grade == 'C':

grade = int(2)

if grade == 'D':

grade = int(1)

if grade == 'F':

grade = int(0)

units = int(input("How many units was the course? "))

totunits += units

points = grade*units

totpoints += points

GPA = totpoints / totunits

print("GPA is ", ("%.2f" % GPA))

print("total points = ", totpoints)

print("total units = ", totunits)

---

One problem that I had with your answer is that we do not, in fact, know how many GPAs will be calculated, nor how many courses will be included in each. This is why I was wondering about a sentinel, but I (think I) figured out how to use the "internal sentinel" of the for loop that you mentioned.

I'm very interested in your matrix though, as I am just getting into studying arrays. Can you help me understand how using such a matrix would be advantageous over doing it the way I did? What would the for loop look like that would do the calculations?

Thanks again for your commitment to teaching!

p.s. any other notes you can offer about the program I wrote, in terms of better practices / ways to make it more efficient, would be greatly appreciated

Some comments to your code:

```
GPA = totpoints / totunits
```

You are lucky here because you use Python 3. If the code was back-ported to Python 2, the results would be unexpected (integer instead of the float; the same problem would be observed in the C or C++ languages). The reason is that the division / needs at least one float to produce float.```
grade = int(3)
```

This is unneccessarily complicated. You simply can assign Whenever you need to map certain constants to some value (like 'A' to 4), you should use a mapping data structure. In Python it is a dictionary:

```
points = { 'A': 4, 'B': 3, 'C': 2, 'D': 1, 'F': 0 }
```

You can use float values for the points if you know the code should be backported to Python 2 (hence the division will produce float).Whenever you write a serie of

Study the following solution and think about how to get the grade_matrix:

```
#!python3
grade_matrix = [ # unknown number of units
['A', 'A', 'B', 'D'], # student 1
['A', 'C', 'A', 'D', 'B', 'A'], # student 2
['B', 'D', 'B', 'D', 'A', 'A', 'B'], # student 3
['B', 'A', 'A', 'B', 'A'], # student 5
['A', 'F', 'B'], # student 4
['C', 'A'], # student 6
['A'], # student 7
['A', 'C', 'D', 'C'], # student 8
['B', 'B', 'F', 'A', 'A', 'A', 'B', 'A', 'A', 'B'], # student 9
['C', 'F', 'F', 'B', 'B', 'B', 'A', 'B', 'A', 'A', 'B'], # student 10
['F', 'D', 'D', 'C', 'A', 'F', 'F'], # student 11
]
# A dictionary: unit grade --> points
points = { 'A': 4, 'B': 3, 'C': 2, 'D': 1, 'F': 0 }
# Loop through all students.
for student_grades in grade_matrix:
totunits = 0 # init
for grade in student_grades:
totunits += points[grade] # add points for the unit
gpa = totunits / len(student_grades)
print(gpa) # another student was processed
```

When I first looked at your solution, I was perplexed by the fact that neither student_grades nor grade was declared/initialized anywhere. They don't need to be, however, because of their implementation in the for loop: On line 21, you could write "for silly_geese in grade_matrix," and the program would know you were referring to the same lists. Likewise, on line 24, you could call each item in each list anything and Python would "bind" the items in those lists to that name, correct? This seems like a really simple realization, but it was very helpful one (for the record, I wrote this paragraph intending to ask 3 different questions, but in trying to verbalize them I answered each).

So, to implement the dictionary to "convert" one value to another that is assigned to it (in this case letter grades to a numeric points), the syntax is: nameOfDictionary[nameOfKey

To take this a step further: Your solution assumes that all courses have the same number of units. This is what enabled you to use len(student_grades) as the divisor in line 26 (correct?) What if we cannot make this assumption, and each grade is "weighted" differently?

I assume we would either want to:

a) create a second matrix, with a unit value for each grade in each student_grades list, and then get the program to multiply each number of points by its corresponding number of units, before dividing by the total number of units to produce a gpa.

b) Use a single matrix, but replace single letter grades with nested pairs containing both the letter grade and the number of units.

Which, if either, of these solutions would be best? How could it be implemented?

Thanks again..

No. At least I do not know if it does.

> nameOfDictionary[nameOfKey

Yes.

> Your solution assumes that all courses have the same number of units. This is what enabled you to use len(student_grades) as the divisor in line 26 (correct?)

The stutent_grades is a list of marks. I do not know if they are mark from one course or marks assigned to more courses. In the later case, the structure is not good as you cannot say what courses were attended.

The len(student_grades) is simply the number of elements of the list.

> What if we cannot make this assumption, and each grade is "weighted" differently?

I do not understand. Do you mean that 'A' can be 8 sometimes? Then it is another task.

link to new question

Sorry, the language I used was confusing. I meant what if each course has a different number of units (hours). For instance, right now I'm taking two 5-hour courses and one 4-hour course. The point value for each letter grade is the same, but the grade I earn in the 4-hour course will affect my GPA slightly less than the grade I earn in the 5-hour courses. My GPA will be calculated as:

((points*5)+(points*5)+(po

So, along with the grade earned, the program would also have to collect the number of units of each course entered. I think we would either want to:

a) create a second matrix, with a unit value corresponding to each grade in each student_grades list. If so, how do we get Python to multiply items in one matrix by the corresponding items in another? (this seems like it'd be a powerful tool in general)

b) use a single matrix with nested pairs for each course containing the grade and unit value ( [('A', 4), ('B', 3)] , etc). If so, how do we get Python to multiply the first item in each pair by the second. Would it be as simple as:

for (x, y) in single_matrix:

points = x*y

totpoints += points

totunits += y

GPA = totpoints / totunits

Which of these strategies do you think would work best. Is there another you think would work better?

Python

From novice to tech pro — start learning today.

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get this solution by purchasing an Individual license!
Start your 7-day free trial.

whileloop in Python. The solution depends on how do you capture the student/course grades. It can be a matrix like that:Open in new window

which is a list of lists. When looping through the container or whatever iterable structure, theforloop knows when the iterator has no more values. This is your internal sentinel. The iterated structure signals to theforloop the end of iteration via the exceptionStopIteration. It is processed internally by theforloop.You need the

whileloop only when a logical condition (boolean expression) is used for decisioncontinue/stop. This is not the case because it is clear how many students you have and what courses the student attended.As you need to calculate the course GPA, the matrix must contain the same number of values/courses for each student. If the student did not attend the course, it must be marked using some special value.

For the above matrix, my solution calculates the GPAs for students:

Open in new window

and the GPAs for coursesOpen in new window

(But I did not checked it. I may have error in the solution.)But the task probably is to calculate only the student grades, which is quite straightforwared. In the case, the matrix need not to be regular (I mean, each student may have different number of marks).