0

My grade calculator needs the restrictions at the top. If the number entered does not fall into the restrictions the program needs to display the "please try again message". I am new to loops so help would be appreciated

MIN_GPA = 0 
MAX_GPA = 4.05
F_grade = 0
D_grade = 0.85
D_plus_grade = 1.15
C_minus_grade = 1.5
C_grade = 1.85
C_plus_grade = 2.15
B_minus_grade = 2.5
B_grade = 2.85
B_plus_grade = 3.15
A_minus_grade = 3.5
A_grade = 3.85
A_plus_grade = 4.05
tgp = float(input("Enter your GPA (0-4):" )) # tgp stands for "Term grade point"

while (tgp<=MAX_GPA) and (tgp >= MIN_GPA):
    if (tgp >= F_grade and tgp <D_grade):
        print ("Your GPA is: ", '%.1f'%tgp, "and your letter grade is F")
    elif (tgp >= D_grade and tgp <D_plus_grade):   
        print ("Your GPA is: ", '%.1f'%tgp, "and your letter grade is D")
    elif (tgp >= D_plus_grade and tgp <C_minus_grade ):
        print ("Your GPA is: ", '%.1f'%tgp, "and your letter grade is D+")
    elif (tgp >= C_minus_grade and tgp <C_grade ):
        print ("Your GPA is: ", '%.1f'%tgp, "and your letter grade is C-")
    elif (tgp >= C_grade and tgp <C_plus_grade):
        print ("Your GPA is: ", '%.1f'%tgp, "and your letter grade is C")
    elif (tgp >= C_plus_grade and tgp <B_minus_grade):
        print ("Your GPA is: ", '%.1f'%tgp, "and your letter grade is C+")
    elif (tgp >= B_minus_grade and tgp <B_grade):
        print ("Your GPA is: ", '%.1f'%tgp, "and your letter grade is B-")
    elif (tgp >= B_grade and tgp <B_plus_grade):
        print ("Your GPA is: ", '%.1f'%tgp, "and your letter grade is B")
    elif (tgp >= B_plus_grade and tgp <A_minus_grade):
        print ("Your GPA is: ", '%.1f'%tgp, "and your letter grade is B+")
    elif (tgp >= A_minus_grade and tgp <A_grade):
        print ("Your GPA is: ", '%.1f'%tgp, "and your letter grade is A-")
    elif (tgp >= A_grade and tgp <A_plus_grade):
        print ("Your GPA is: ", '%.1f'%tgp, "and your letter grade is A")
    elif (tgp == A_plus_grade):
        print ("Your GPA is: ", '%.1f'%tgp, "and your letter grade is A+")
    else:
        print ("\nPlease enter a valid input. Please try again.\n")
        continue
    break
  • Welcome to Stack Overflow. Where the code says `while (tgp<=MAX_GPA) or (tgp >= MIN_GPA):` - can you give me an example of a number that you think *doesn't* meet this condition? – Karl Knechtel May 17 '22 at 22:05
  • When a number is greater than 4.05 or less than 0.0- I changed the expression to "and" instead of "or" – Jozsef Babits May 17 '22 at 22:06
  • "# tgp stands for "Term grade point"" If you have to explain it in the comments, then why write it that way? The messages that you show to the user call it a GPA. Why not use `gpa` as the variable name, to match that? – Karl Knechtel May 17 '22 at 22:06
  • I can change the variable name if it makes it easier to understand – Jozsef Babits May 17 '22 at 22:08
  • Also I figured it out, thanks for the input! – Jozsef Babits May 17 '22 at 22:09

0 Answers0