1. 程式人生 > 其它 >大爽Python入門教程 5-5 實踐演示 控制檯版本——簡易成績管理系統

大爽Python入門教程 5-5 實踐演示 控制檯版本——簡易成績管理系統

大爽Python入門公開課教案 點選檢視教程總目錄

1 情景思考

情景介紹

假設有一位老師,帶著一堆學生。
學生的成績儲存在scores.txt中,具體內容如下

Smith, 84
Anderson, 91
Clark, 69
ZhangSan, 82
Allen, 90
Green, 91
Lee, 85
Scott, 71
Martin, 65
Wang, 91
Taylor, 88

現在希望有一個簡易的成績管理系統(控制檯版本的),
能幫助該老師查詢、修改、展示成績。

具體要求如下
具體需要以下幾個頁面(選單)

以下提供了中文版的和英文版的,
挑選一個自己習慣的就好。

主介面

輸出主要的介紹資訊後,讀取使用者輸入。

英文版本

---------------
Welcome!
You can enter `f`, `u`, `l` or `e`.
The meaning is as follows
- f: filter student by a score
- u: update score
- l: show all student with scores
- e: exit
Enter your command:

中文版本

---------------
歡迎!你可以輸入`f`、`u`、`l`、`e`命令, 意義如下:
- f: 查詢大於等於某成績的所有學生
- u: 修改學生分數
- l: 展示所有學生分數
- e: 退出
請輸入命令:

輸入e,輸出再見Bye,儲存成績,然後退出。
輸入fcul命令,進入對應頁面。
輸入不符合規範,提示輸入無效Invalid Input.
並重新展示主介面。

執行完具體命令後,也重新展示下主介面。

展示所有分數介面

英文版如下

Students Scores:
Smith          : 84
Anderson       : 91
Clark          : 69
ZhangSan       : 82
Allen          : 90
Green          : 91
Lee            : 85
Scott          : 71
Martin         : 65
Wang           : 91
Taylor         : 88

注:名字要對齊(左對齊),長度為15。

中文版,將Students Scores:改成學生成績即可

查詢介面

英文版

Enter filter score:

假設輸入85, 輸出如下

Students Scores:
Anderson       : 91
Allen          : 90
Green          : 91
Lee            : 85
Wang           : 91
Taylor         : 88

中文版

請輸入查詢分: 85
學生成績:
Anderson       : 91
Allen          : 90
Green          : 91
Lee            : 85
Wang           : 91
Taylor         : 88

修改學生分數介面

英文版

Enter student name: Clark
Enter student score: 75
Updated successfully.

中文版

請輸入學生名字: Clark
請輸入學生分數: 75
修改成功

2 實現過程展示

程式碼框架設計

主選單用主函式去處理
然後每個命令對應一個函式
初步佈局如下

def read_scores(score_file):
    pass


def show_all(scores, filter_value=0):
    pass


def filter_score(scores):
    pass


def update_score(scores):
    pass


def save(score_file, scores):
    pass


def main(score_file):
    pass

定義常量

在程式碼開頭定義好所有需要的常量
包括選單文字和檔案路徑

SCORE_FILE = "scores.txt"

MAIN_MENU = """---------------
Welcome!
You can enter `f`, `u`, `l` or `e`.
The meaning is as follows
- f: filter student by a score
- u: update score
- l: show all student with scores
- e: exit
Enter your command: """
INVALID = "Invalid Input."

FILTER_MENU1 = "Enter filter score: "
UPDATE_MENU1 = "Enter student name: "
UPDATE_MENU2 = "Enter student score: "
UPDATE_MENU3 = "Updated successfully."
SHOW_MENU1 = "Students Scores: "
EXIT_MENU = "Bye"

注:個人喜歡將常量用大寫加下劃線命名。

後面的中文版本,只需要將常量這裡的文字改成中文即可。
英文版和中文版之後的邏輯程式碼是一樣的。
即如下

SCORE_FILE = "scores.txt"

MAIN_MENU = """---------------
歡迎!你可以輸入`f`、`u`、`l`、`e`命令, 意義如下:
- f: 查詢大於等於某成績的所有學生
- u: 修改學生分數
- l: 展示所有學生分數
- e: 退出
請輸入命令: """
INVALID = "輸入無效"

FILTER_MENU1 = "請輸入查詢分: "
UPDATE_MENU1 = "請輸入學生名字: "
UPDATE_MENU2 = "請輸入學生分數: "
UPDATE_MENU3 = "修改成功."
SHOW_MENU1 = "學生成績: "
EXIT_MENU = "再見"

實現主函式main並呼叫

程式碼如下

def main(score_file):
    scores = read_scores(score_file)
    while True:
        cmd = input(MAIN_MENU)
        if cmd == "f":
            filter_score(scores)
        elif cmd == "u":
            update_score(scores)
        elif cmd == "l":
            show_all(scores)
        elif cmd == "e":
            save(score_file, scores)
            print(EXIT_MENU)
            break
        else:
            print(INVALID)

然後在函式之外,程式碼最後面,呼叫主函式,程式碼如下

main(SCORE_FILE)

實現read_scores

程式碼如下

def read_scores(score_file):
    scores = {}

    with open(score_file, 'r') as f:
        fr = f.read()

    lines = fr.split("\n")
    for line in lines:
        line = line.strip()
        if line:
            name, score = line.split(",")
            score = int(score)
            scores[name] = score

    return scores

實現show_all

程式碼如下

def show_all(scores, filter_value=0):
    print(SHOW_MENU1)
    for name in scores:
        score = scores[name]
        if score >= filter_value:
            print("{:<15s}: {}".format(name, score))

實現filter_score

程式碼如下

def filter_score(scores):
    while True:
        score = input(FILTER_MENU1)
        if score.isdigit():
            score = int(score)
            show_all(scores, filter_value=score)
            return
        else:
            print(INVALID)

實現update_score

程式碼如下

def update_score(scores):
    while True:
        name = input(UPDATE_MENU1)
        if name not in scores:
            print(INVALID)
        else:
            break

    while True:
        score = input(UPDATE_MENU2)
        if score.isdigit():
            score = int(score)
            scores[name] = score
            print(UPDATE_MENU3)
            return
        else:
            print(INVALID)

實現save

程式碼如下

def save(score_file, scores):
    with open(score_file, 'w') as f:
        for name in scores:
            score = scores[name]
            f.write("{},{}\n".format(name, score))

3 總程式碼

英文版

SCORE_FILE = "scores.txt"

MAIN_MENU = """---------------
Welcome!
You can enter `f`, `u`, `l` or `e`.
The meaning is as follows
- f: filter student by a score
- u: update score
- l: show all student with scores
- e: exit
Enter your command: """
INVALID = "Invalid Input."

FILTER_MENU1 = "Enter filter score: "
UPDATE_MENU1 = "Enter student name: "
UPDATE_MENU2 = "Enter student score: "
UPDATE_MENU3 = "Updated successfully."
SHOW_MENU1 = "Students Scores: "
EXIT_MENU = "Bye"


def read_scores(score_file):
    scores = {}

    with open(score_file, 'r') as f:
        fr = f.read()

    lines = fr.split("\n")
    for line in lines:
        line = line.strip()
        if line:
            name, score = line.split(",")
            score = int(score)
            scores[name] = score

    return scores


def show_all(scores, filter_value=0):
    print(SHOW_MENU1)
    for name in scores:
        score = scores[name]
        if score >= filter_value:
            print("{:<15s}: {}".format(name, score))


def filter_score(scores):
    while True:
        score = input(FILTER_MENU1)
        if score.isdigit():
            score = int(score)
            show_all(scores, filter_value=score)
            return
        else:
            print(INVALID)


def update_score(scores):
    while True:
        name = input(UPDATE_MENU1)
        if name not in scores:
            print(INVALID)
        else:
            break

    while True:
        score = input(UPDATE_MENU2)
        if score.isdigit():
            score = int(score)
            scores[name] = score
            print(UPDATE_MENU3)
            return
        else:
            print(INVALID)


def save(score_file, scores):
    with open(score_file, 'w') as f:
        for name in scores:
            score = scores[name]
            f.write("{},{}\n".format(name, score))


def main(score_file):
    scores = read_scores(score_file)
    while True:
        cmd = input(MAIN_MENU)
        if cmd == "f":
            filter_score(scores)
        elif cmd == "u":
            update_score(scores)
        elif cmd == "l":
            show_all(scores)
        elif cmd == "e":
            save(score_file, scores)
            print(EXIT_MENU)
            break
        else:
            print(INVALID)


main(SCORE_FILE)

中文版

將前面的文字常量改掉就行,如下

SCORE_FILE = "scores.txt"

MAIN_MENU = """---------------
歡迎!你可以輸入`f`、`u`、`l`、`e`命令, 意義如下:
- f: 查詢大於等於某成績的所有學生
- u: 修改學生分數
- l: 展示所有學生分數
- e: 退出
請輸入命令: """
INVALID = "輸入無效"

FILTER_MENU1 = "請輸入查詢分: "
UPDATE_MENU1 = "請輸入學生名字: "
UPDATE_MENU2 = "請輸入學生分數: "
UPDATE_MENU3 = "修改成功."
SHOW_MENU1 = "學生成績: "
EXIT_MENU = "再見"

後面的邏輯程式碼是一樣的,不用改。

4 執行效果

英文版

---------------
Welcome!
You can enter `f`, `u`, `l` or `e`.
The meaning is as follows
- f: filter student by a score
- u: update score
- l: show all student with scores
- e: exit
Enter your command: l
Students Scores:
Smith          : 84
Anderson       : 91
Clark          : 69
ZhangSan       : 82
Allen          : 90
Green          : 91
Lee            : 85
Scott          : 71
Martin         : 65
Wang           : 91
Taylor         : 88
---------------
Welcome!
You can enter `f`, `u`, `l` or `e`.
The meaning is as follows
- f: filter student by a score
- u: update score
- l: show all student with scores
- e: exit
Enter your command: f
Enter filter score: 85
Students Scores:
Anderson       : 91
Allen          : 90
Green          : 91
Lee            : 85
Wang           : 91
Taylor         : 88
---------------
Welcome!
You can enter `f`, `u`, `l` or `e`.
The meaning is as follows
- f: filter student by a score
- u: update score
- l: show all student with scores
- e: exit
Enter your command: u
Enter student name: Clark
Enter student score: 75
Updated successfully.
---------------
Welcome!
You can enter `f`, `u`, `l` or `e`.
The meaning is as follows
- f: filter student by a score
- u: update score
- l: show all student with scores
- e: exit
Enter your command: l
Students Scores:
Smith          : 84
Anderson       : 91
Clark          : 75
ZhangSan       : 82
Allen          : 90
Green          : 91
Lee            : 85
Scott          : 71
Martin         : 65
Wang           : 91
Taylor         : 88
---------------
Welcome!
You can enter `f`, `u`, `l` or `e`.
The meaning is as follows
- f: filter student by a score
- u: update score
- l: show all student with scores
- e: exit
Enter your command: e
Bye.

中文版

---------------
歡迎!你可以輸入`f`、`u`、`l`、`e`命令, 意義如下:
- f: 查詢大於等於某成績的所有學生
- u: 修改學生分數
- l: 展示所有學生分數
- e: 退出
請輸入命令: l
學生成績:
Smith          : 84
Anderson       : 91
Clark          : 69
ZhangSan       : 82
Allen          : 90
Green          : 91
Lee            : 85
Scott          : 71
Martin         : 65
Wang           : 91
Taylor         : 88
---------------
歡迎!你可以輸入`f`、`u`、`l`、`e`命令, 意義如下:
- f: 查詢大於等於某成績的所有學生
- u: 修改學生分數
- l: 展示所有學生分數
- e: 退出
請輸入命令: f
請輸入查詢分: 85
學生成績:
Anderson       : 91
Allen          : 90
Green          : 91
Lee            : 85
Wang           : 91
Taylor         : 88
---------------
歡迎!你可以輸入`f`、`u`、`l`、`e`命令, 意義如下:
- f: 查詢大於等於某成績的所有學生
- u: 修改學生分數
- l: 展示所有學生分數
- e: 退出
請輸入命令: u
請輸入學生名字: Clark
請輸入學生分數: 75
修改成功.
---------------
歡迎!你可以輸入`f`、`u`、`l`、`e`命令, 意義如下:
- f: 查詢大於等於某成績的所有學生
- u: 修改學生分數
- l: 展示所有學生分數
- e: 退出
請輸入命令: l
學生成績:
Smith          : 84
Anderson       : 91
Clark          : 75
ZhangSan       : 82
Allen          : 90
Green          : 91
Lee            : 85
Scott          : 71
Martin         : 65
Wang           : 91
Taylor         : 88
---------------
歡迎!你可以輸入`f`、`u`、`l`、`e`命令, 意義如下:
- f: 查詢大於等於某成績的所有學生
- u: 修改學生分數
- l: 展示所有學生分數
- e: 退出
請輸入命令: e
再見