Python課後練習題:編寫程序輸入一個學生成績,使用assert斷言處理分數不合理的情況

輸入一個學生成績,並轉換爲A優秀、B良好、C合格、D不及格的形式,將學生成績打印出來。要求使用assert斷言處理分數不合理的情況。

while True:
    try:
        score = int(input("請輸入你的成績:"))
        assert score >= 0 and score <= 100, "輸入有誤請重新輸入!"
        if score >= 90:
            print('成績爲:A')
        elif score >= 80 and score <= 89:
            print('成績爲:B')
        elif score >= 60 and score <= 79:
            print('成績爲:C')
        else:
            print('成績爲:D,不及格!!!')
    except AssertionError as reason:
        print(reason)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章