NWC42 NWC42 - 2 months ago 4x
Python Question

Simplifying, printing text instead of boolean value, Python

Using python to grade student scores. Trying to simplify from

A = ((float(scptratio)) >= (0.9) and (float(scptratio) <= 1))
B = ((float(scptratio)) >= (0.8) and (float(scptratio) <= (0.89)))

if A == True:
print (studentname, (" has scored an A on the test."))

elif B == True:
print (studentname, (" has scored an B on the test."))

elif C == True:
print (studentname, (" has scored an C on the test."))

etc. to something along the lines of

A = (((float(scptratio)) >= (0.9) and (float(scptratio) <= 1)), "A")
B = (((float(scptratio)) >= (0.8) and (float(scptratio) <= (0.89))), "B")

Pass = [A, B, C, D]

if Pass:
print (studentname, "passed the test with a coefficient of", scptratio, ("scoring a grade of {}".format(Pass)))
elif F:
print (studentname, "has failed the test.")

print ("Error! Negative value entered.")

How can i get it to print the actual letter score instead of boolean values?


I'd suggest making an easily-editable list containing the minimum score required for a specific grade, from high to low:

minimum_scores = [("A", 9), ("B", 8), ("C", 7), ("D", 6)]

Then go through this list and print the first grade for which the student passed.

for grade, score in minimum_scores:
    if float(scptratio) >= score:
        print(studentname, "passed the test with a coefficient of", str(scptratio), "scoring a grade of", grade)
else: #No break hit, so the student didn't pass any of the minimum requirements.
    print(studentname, "has failed the test.")