2016-08-14 76 views
2

使用python对学生成绩进行评分。试图从简化,打印文本而不是布尔值,Python

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.")) 

等简化的东西沿着

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.") 

else: 
    print ("Error! Negative value entered.") 

行我怎样才能得到它打印的实际字母比分反超布尔值吗?

+0

你真的不应该用大写字母命名你的变量,因为它们可能会与类名冲突。 – Soviut

+0

您可以通过在脚本开始处将'scpratio'转换为float **一次**来大幅提高可读性。 –

+0

谢谢,虽然它通过,但真的很快乐。 – NWC42

回答

1

我建议让含有特定等级所要求的最低分数容易编辑列表,从高至低:

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

然后再通过这个名单,并打印一年级为其学生通过。

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) 
     break 
else: #No break hit, so the student didn't pass any of the minimum requirements. 
    print(studentname, "has failed the test.")