大爽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
再见
posted @ 2021-11-23 18:15  大爽歌python编程辅导  阅读(173)  评论(0编辑  收藏  举报