1. 程式人生 > >python二級練習(6)

python二級練習(6)

6. 從鍵盤接收一百分制成績(0~100),要求輸出其對應的成績等級A~E。其中,90分以上為'A',80~89分為'B',70~79分為'C',60~69分為'D',60分以下為'E'。

#python 3.6
#蔡軍生 
#http://blog.csdn.net/caimouse/article/details/51749579
#

dictionary = {90:'A', 80:'B', 70:'C', 60:'D', 0: 'E'}

score = int(input('請輸入成績(0~100):'))
if score > 100 or score < 0:
    print('輸入錯誤成績')
else:
    for key in sorted(dictionary.keys(),  reverse = True):
        if score >= key:
            print('成績為', dictionary[key])
            break
輸出結果:



========== RESTART: D:/work/example/py_ex_006.py ==========
請輸入成績(0~100):60
成績為 E
>>> 
========== RESTART: D:/work/example/py_ex_006.py ==========
請輸入成績(0~100):70
成績為 C
>>> 
========== RESTART: D:/work/example/py_ex_006.py ==========
請輸入成績(0~100):1010
輸入錯誤成績
>>> 

Python遊戲開發入門