2016-08-14 77 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.")