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 while loop to calculate multiple GPAs and a while loop to collect individual grades (i.e. a nested while loop).

For your demo, calculate the GPA to 2 decimal places for these two course combinations:

 First Case:                5 units of A           4 units of B           3 units of C

 Second Case:           6 units of A           6 units of B           4 units of C

I have no idea what to do.

Recommended Answers

All 3 Replies

I have no idea what to do.

In this case, compute the GPA by hand, without your computer, and write down carefully every detail of your procedure. This should give you the algorithm.

I have no idea what to do.

The underlying calculations are pretty simple but I myself am confused about what the other stuff means. You almost have to RTTM (Read The Teacher's Mind). For example:

... For your demo, calculate the GPA to 2 decimal places for these two course combinations: ...

Now does that mean you are supposed to use Python's Decimal module here? I rather doubt it, I think teacher wants you to print out the GPA to 2 decimal places. But you never can tell -- if you've been studying Decimal then maybe so.

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

Does this mean you are supposed to hand-enter the raw data every time you run the program? What a pain that would be!

Personally I would store the data in the program, unless I had good reason to believe that was wrong. One possibility is a dictionary, viz:

all_grades = [   \
{'A':5, 'B':4, 'C':3, 'Case':'First'},   \
{'A':6, 'B':6, 'C':4, 'Case':'Second'}  ]

This may not be the best approach as there are other ways to go, e.g., named tuples, but dictionaries are probably learned earlier in the course.

Then one while loop would iterate over all_grades, processing them one dictionary at a time. It's the processing part that has the inner while loop. Frankly I'd use for loops, not while loops, but you can convert between them fairly easily.

Here's a, uhhh, trick that may save some coding. If you have a grade in variable grade, then "FDCBA".index(grade.upper()) will return the number of points for that grade, i.e., 0 for 'F', 3 for 'B', and so on. You would need to multiply this by the unit count, but I'm sure you can handle that part.

What I meant by 'no idea' was that I wasn't sure what to with python. I followed your advice, and it made me remember some of the syntax in python.
I got:

stop1 = int(input('How many GPAs do want to calculate? '))
totalgp = 0

totalgp1 = 0

while stop1 > 0 :

stop = int(input('Enter the number of classes you have: '))
    while stop > 0:
        grade = input('Enter the grade recieved: ')
        if grade == 'A':
            grade = 4

        elif grade == 'B':
            grade = 3

        elif grade == 'C':
            grade = 2

        elif grade == 'D':
            grade = 1

        else:
            grade = 0
        units = int(input('Enter the amount of units for that class: '))

        GP = grade * units
        totalgp += GP
        totalgp1 += units
        stop -= 1



    GPA = round(totalgp/totalgp1, 2)
    print('Gpa: ', GPA)
    stop1 -= 1

which completes the assignment. Is there any way to clean this up?

Also, I have another question:

How do I restrict this algorithm/code to only take in values from 0-101

T =         # Total points added

GC =        # the number of grades entered
grade = input("Enter a grade, 999 to stop:")    # the number of points of a grade 0-101
grade = int(grade)

while grade != 999:

    if grade < 0 or grade > 101:
        print('retry')

    T = T + grade
    GC = GC + 1
    grade = int(input("Another grade:"))



if GC != 0:
    average = round(float(T)/GC,2)
    print ("The number of scores:", GC,'\n',
           'The average is', average)

thanks for previous help and future help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.