Python基礎案例練習:制作學生管理系統


一.前言

學生管理系統,相信大家或多或少都有做過

最近看很多學生作業都是制作一個學生管理系統

於是,今天帶大家做一個簡單的學生管理系統

二.開發環境:

我用到的開發環境

  • Python 3.8
  • Pycharm 2021.2

三.涉及知識點

  • Python基礎語法
  • 基本的數據類型與結構
  • 基本的邏輯控制語句
  • 實戰小項目

四.接下來我們開始敲代碼

我們一步步來完成學生管理系統

第一步:制作學生管理系統的界面

  1. 程序啟動,顯示信息管理系統歡迎界面,並顯示功能菜單 (print)
  2. 用戶用數字選擇不同的功能 (input)
  3. 根據功能選擇,執行不同的功能 (if 判斷 多分支選擇)
  4. 需要記錄學生的 姓名、語文成績、數學成績、英語成績 、總分 (input, 數據容器存儲輸入的學生信息)
  5. 如果查詢到指定的學生信息,用戶可以選擇 修改 或者 刪除 信息 (多分支選擇里面的邏輯)
  6. 進入或退出時加載或保存數據 (文件操作)
"""
str_info = """**************************************************
歡迎使用【學生信息管理系統】V1.0
請選擇你想要進行的操作
1. 新建學生信息
2. 顯示全部信息
3. 查詢學生信息
4. 刪除學生信息
5. 修改學生信息

0. 退出系統
**************************************************"""


while True:
    # 1. 程序啟動,顯示信息管理系統歡迎界面,並顯示功能菜單 (print)
    print(str_info)
    # 2.用戶用數字選擇不同的功能(input)
    action = input('請選擇你要進行的操作(輸入數字):')
    if action == '1':
        print('1. 新建學生信息')
    elif action == '2':
        print('2. 顯示全部信息')
    elif action == '3':
        print('3. 查詢學生信息')
    elif action == '4':
        print('4. 刪除學生信息')
    elif action == '5':
        print('5. 修改學生信息')
    elif action == '0':
        print('0. 退出系統')
        break
    else:
        print('請輸入正確的選項!')

效果:

**************************************************
歡迎使用【學生信息管理系統】V1.0
請選擇你想要進行的操作
1. 新建學生信息
2. 顯示全部信息
3. 查詢學生信息
4. 刪除學生信息
5. 修改學生信息

0. 退出系統
**************************************************

第二步:新建學生信息

需要記錄學生的 姓名、語文成績、數學成績、英語成績 、總分 (input, 數據容器存儲輸入的學生信息)

name = input('請輸入學生的姓名:')
chinese = int(input('請輸入學生的語文成績:'))
math = int(input('請輸入學生的數學成績:'))
english = int(input('請輸入學生的英語成績:'))

total = chinese + math + english

# 用什么數據容器接受比較好?  存儲信息, 取值  采用字典
students = [
    {'name': name, 'chinese': chinese, 'math': math, 'english': english, 'total': total}
]

print(students)

第三步:顯示全部學生信息

students = [
    {'name': '正心', 'chinese': 59, 'math': 59, 'english': 59, 'total': 177},
    {'name': '張三', 'chinese': 59, 'math': 59, 'english': 59, 'total': 177},
    {'name': '李四', 'chinese': 59, 'math': 59, 'english': 59, 'total': 177},
    {'name': '王五', 'chinese': 59, 'math': 59, 'english': 59, 'total': 177},
]

print('姓名\t語文\t數學\t英語\t總分')

for stu in students:
    # print(stu)
    print(f'{stu["name"]}\t{stu["chinese"]}\t\t{stu["math"]}\t\t{stu["english"]}\t\t{stu["total"]}')

第四步:查詢學生信息

students = [
    {'name': '正心', 'chinese': 59, 'math': 59, 'english': 59, 'total': 177},
    {'name': '張三', 'chinese': 59, 'math': 59, 'english': 59, 'total': 177},
    {'name': '李四', 'chinese': 59, 'math': 59, 'english': 59, 'total': 177},
    {'name': '王五', 'chinese': 59, 'math': 59, 'english': 59, 'total': 177},
]

# 5. 如果查詢到指定的學生信息,用戶可以選擇 修改 或者 刪除 信息 (多分支選擇里面的邏輯)

name = input('請輸入你要查詢學生的姓名:')
# 更多Python相關視頻、資料加群778463939免費獲取
# 先遍歷所有學生
for stu in students:
    # 如果滿足條件, 就是查詢到了
    if name == stu['name']:
        print('姓名\t語文\t數學\t英語\t總分')
        print(f'{stu["name"]}\t{stu["chinese"]}\t\t{stu["math"]}\t\t{stu["english"]}\t\t{stu["total"]}')
        # 一旦查詢到了就停止查詢
        break

else:
    # 沒找到
    print('該學生不存在, 請檢查名字是否輸入正確!')

第五步:修改學生信息

import pprint

students = [
    {'name': '正心', 'chinese': 59, 'math': 59, 'english': 59, 'total': 177},
    {'name': '張三', 'chinese': 59, 'math': 59, 'english': 59, 'total': 177},
    {'name': '李四', 'chinese': 59, 'math': 59, 'english': 59, 'total': 177},
    {'name': '王五', 'chinese': 59, 'math': 59, 'english': 59, 'total': 177},
]

# 5. 如果查詢到指定的學生信息,用戶可以選擇 修改 或者 刪除 信息 (多分支選擇里面的邏輯)

name = input('請輸入你要修改學生的姓名:')

# 先遍歷所有學生
for stu in students:
    # 如果滿足條件, 就是查詢到了, 找到了這個學生
    # 找到了需要修改的學生
    if name == stu['name']:
        # 不想修改, 直接回車
        print('(如果不想修改,直接回車!)')
        name = input('請重新輸入學生的姓名:')
        chinese = input('請重新輸入學生的語文成績:')
        math = input('請重新輸入學生的數學成績:')
        english = input('請重新輸入學生的英語成績:')
        # total = chinese + math + english

        # 用戶輸入了數據才修改
        if name:
            stu['name'] = name
        if chinese:
            stu['chinese'] = int(chinese)
        if math:
            stu['math'] = int(math)
        if english:
            stu['english'] = int(english)

        stu['total'] = stu['chinese'] + stu['math'] + stu['english']

        break

else:
    # 沒找到
    print('該學生不存在, 請檢查名字是否輸入正確!')

pprint.pprint(students)

第六步: 刪除學生信息

import pprint

students = [
    {'name': '正心', 'chinese': 59, 'math': 59, 'english': 59, 'total': 177},
    {'name': '張三', 'chinese': 59, 'math': 59, 'english': 59, 'total': 177},
    {'name': '李四', 'chinese': 59, 'math': 59, 'english': 59, 'total': 177},
    {'name': '王五', 'chinese': 59, 'math': 59, 'english': 59, 'total': 177},
]

# 5. 如果查詢到指定的學生信息,用戶可以選擇 修改 或者 刪除 信息 (多分支選擇里面的邏輯)
# 更多Python相關視頻、資料加群778463939免費獲取
name = input('請輸入你要刪除學生的姓名:')

# 先遍歷所有學生
for stu in students:
    # 找到學生
    if name == stu['name']:
        # 刪除學生
        students.remove(stu)
        break

else:
    # 沒找到
    print('該學生不存在, 請檢查名字是否輸入正確!')

pprint.pprint(students)

然后把他們拼接起來,就完成了我們簡單的一個學生管理系統!

五、最后代碼

import json

str_info = """**************************************************
歡迎使用【學生信息管理系統】V1.0
請選擇你想要進行的操作
1. 新建學生信息
2. 顯示全部信息
3. 查詢學生信息
4. 刪除學生信息
5. 修改學生信息

0. 退出系統
**************************************************"""

# 讀取文件
with open('students.json', mode='r', encoding='utf-8') as f:
    students_str = f.read()

students = json.loads(students_str)

while True:
    # 1. 程序啟動,顯示信息管理系統歡迎界面,並顯示功能菜單 (print)
    print(str_info)
    # 2.用戶用數字選擇不同的功能(input)
    action = input('請選擇你要進行的操作(輸入數字):')
    if action == '1':
        print('1. 新建學生信息')
        name = input('請輸入學生的姓名:')
        chinese = int(input('請輸入學生的語文成績:'))
        math = int(input('請輸入學生的數學成績:'))
        english = int(input('請輸入學生的英語成績:'))

        total = chinese + math + english
        # 新的學生
        stu = {'name': name, 'chinese': chinese, 'math': math, 'english': english, 'total': total}
        students.append(stu)

    elif action == '2':
        print('2. 顯示全部信息')
        print('姓名\t語文\t數學\t英語\t總分')

        for stu in students:
            print(f'{stu["name"]}\t{stu["chinese"]}\t\t{stu["math"]}\t\t{stu["english"]}\t\t{stu["total"]}')

    elif action == '3':
        print('3. 查詢學生信息')
        name = input('請輸入你要查詢學生的姓名:')

        # 先遍歷所有學生
        for stu in students:
            # 如果滿足條件, 就是查詢到了
            if name == stu['name']:
                print('姓名\t語文\t數學\t英語\t總分')
                print(f'{stu["name"]}\t{stu["chinese"]}\t\t{stu["math"]}\t\t{stu["english"]}\t\t{stu["total"]}')
                # 一旦查詢到了就停止查詢
                break

        else:
            # 沒找到
            print('該學生不存在, 請檢查名字是否輸入正確!')

    elif action == '4':
        print('4. 刪除學生信息')
        name = input('請輸入你要刪除學生的姓名:')

        # 先遍歷所有學生
        for stu in students:
            # 找到學生
            if name == stu['name']:
                # 刪除學生
                students.remove(stu)
                break

        else:
            # 沒找到
            print('該學生不存在, 請檢查名字是否輸入正確!')
    elif action == '5':
        print('5. 修改學生信息')
        name = input('請輸入你要修改學生的姓名:')

        # 先遍歷所有學生
        for stu in students:
            # 如果滿足條件, 就是查詢到了, 找到了這個學生
            # 找到了需要修改的學生
            if name == stu['name']:
                # 不想修改, 直接回車
                print('(如果不想修改,直接回車!)')
                name = input('請重新輸入學生的姓名:')
                chinese = input('請重新輸入學生的語文成績:')
                math = input('請重新輸入學生的數學成績:')
                english = input('請重新輸入學生的英語成績:')
                # total = chinese + math + english

                # 用戶輸入了數據才修改
                if name:
                    stu['name'] = name
                if chinese:
                    stu['chinese'] = int(chinese)
                if math:
                    stu['math'] = int(math)
                if english:
                    stu['english'] = int(english)

                stu['total'] = stu['chinese'] + stu['math'] + stu['english']
                break
        else:
            # 沒找到
            print('該學生不存在, 請檢查名字是否輸入正確!')
    elif action == '0':
        print('0. 退出系統')
        with open('students.json', mode='w', encoding='utf-8') as f:
            # 把列表對象轉化成字符串  ascii 編碼
            students_str = json.dumps(students, ensure_ascii=False)
            f.write(students_str)
            print(students_str)
        break
    else:
        print('請輸入正確的選項!')

是不是很簡單!歡迎留言討論!

喜歡記得點贊哦!

最后要是代碼看不懂,還有視頻教程
學生管理系統視頻教程地址:←點擊左邊藍色文字就可以跳轉觀看了
Python基礎入門教程推薦:←點擊左邊藍色文字就可以跳轉觀看了


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM