issue Letter Grades on python

Question:

Hi all I’m having an issue with my python code.

Im new to python im study at tafe and this is one of my task for introduction to python

The task is to make a simple code that grades the letters from the total and in oreder for example HD, D, C, P, F here the code I made so far :

#Blank variable to store user input and GPA
totals = set()
letters = set()

#Get and store user input
print ("Please enter the grade for each student, or press q to quit")
while(True):
    user_input = input("> ")
    if(user_input.lower() == "q"):
        break
    totals.add(user_input)

#Caclulate letter grade for each user response
for total in totals:
    if total <= "59":
        letters.add("F")
    elif total <= "69":
        letters.add("P")
    elif total <= "79":
        letters.add("C")
    elif total <= "89":
        letters.add("D")
    else:
        letters.add("HD")


letters = ['HD', 'D', 'C', 'P', 'F']

#Print GPA results.
print("The GPA for your students is: {}".format(letters))

The issue is I’m having is I am trying to duplicate the grades and have them in order for for example if I input 90, 80 , 88 , 75 it should print out like this HD, D, D, C but when I do it ,it only does this HD, D, C

I tried everything but nothing work I’ll look everywhere online but no luck

Any help will be appreciated
Thank you

Asked By: Ze_Watermelon

||

Answers:

Change sets to list and add() to append(), also remove the letters = ['HD', 'D', 'C', 'P', 'F'] at the bottom that is overriding the results.

#Blank variable to store user input and GPA
totals = list()
letters = list()


print ("Please enter the grade for each student, or press q to quit")
while(True):
    user_input = input("> ")
    if(user_input.lower() == "q"):
        break
    totals.append(user_input)

#Caclulate letter grade for each user response
for total in totals:
    if total <= '59':
        letters.append("F")
    elif total <= '69':
        letters.append("P")
    elif total <= '79':
        letters.append("C")
    elif total <= '89':
        letters.append("D")
    else:
        letters.append("HD")


#Print GPA results.
print("The GPA for your students is: {}".format(letters))

As you are doing string comparisons, you should be careful to consider what if the entry is not a number that also is not q. I could enter a grade of "Apples" and it would give a grade of HD. You can also consider doing it as int comparisons, but you would also need to do checks before converting the string to int.

Answered By: Shorn
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.