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.")
行我怎樣才能得到它打印的實際字母比分反超布爾值嗎?
你真的不應該用大寫字母命名你的變量,因爲它們可能會與類名衝突。 – Soviut
您可以通過在腳本開始處將'scpratio'轉換爲float **一次**來大幅提高可讀性。 –
謝謝,雖然它通過,但真的很快樂。 – NWC42