赞
踩
今日主题:入门必写系统程序 "学生管理系统" 。
主要实现的功能:1、新增学生信息 2、删除学生信息 3、修改学生信息 4、查找学生信息 5、查看所有学生信息 6、排序学生成绩 7、统计学生总人数 10、退出系统。通过数字选择菜单功能。
保存到文件中的需要导入一个库。然后程序编写好之后需要跑起来,怎样完成库的导入和主函数调用呢?需要import和主函数main()
os库是Python的标准库之一,它里面包含几百个处理函数,能够处理与操作系统相关的功能,包括路径操作、进程管理、环境参数设置等几类功能。
- import os.path
-
- # 只有语句运行main的时候 这个输出语句才会运、不加的则其他中调用此模块函数 也会输出
- if __name__ == '__main__':
- main()
1、新增学生信息代码模块
- # 新增学生信息
- def increase():
- print("你好,先输入学号查看是否已存在学生信息...")
- id = input("请输入学生学号:")
- while True:
- if os.path.exists(filename):
- with open(filename, 'r', encoding='utf-8') as file:
- student_old = file.readlines()
- with open(filename, 'r', encoding='utf-8') as file:
- students = file.readlines()
- students_new = []
- for item in students:
- d = dict(eval(item))
- students_new.append(d)
- # show_query(students_new)
- for item in student_old:
- d = {}
- d = dict(eval(item))
- if d['学号'] == id:
- print("当前学号已存在,请重新申请学号...")
- continue
- else:
- break
- print("现在为你录入学生基本信息...")
- id = input("请输入学生id:")
- name = input("请输入学生姓名:")
- age = input("请输入学生年龄:")
- gender = input("请输入学生性别:")
- print("OK...现在开始录入成绩...")
- sub1 = int(input("请输入Python成绩:"))
- sub2 = int(input("请输入Java成绩:"))
- sub3 = int(input("请输入MySQL成绩:"))
- stockpile_list.append(
- {"姓名": name, "学号": id, "Python成绩": sub1, "年龄": age, "性别": gender, "Java成绩": sub2, "MySQL成绩": sub3})
- # 调用save函数保存信息
- save(stockpile_list)
- print("添加成功!")
- break
2、删除学生信息代码模块
- # 删除学生信息
- def delete():
- print("请问你想通过姓名删除还是学号删除?")
- answer = input("按ID查询请按1,按姓名查询请按2")
- if answer == "2":
- while True:
- # 通过姓名删除学生信息
- name = input("请输入学生姓名:")
- if name != ' ':
- if os.path.exists(filename):
- with open(filename, 'r', encoding='utf-8') as file:
- student_old = file.readlines()
- else:
- student_old = []
- flag = False
- if student_old: # 空列表等于 False,非空列表等于True
- with open(filename, 'r', encoding='utf-8') as wfile:
- # 只用于写入。如果该文件已存在则打开文件,并从开头开始编辑,即原有内容会被删除
- d = {}
- for item in student_old:
- d = dict(eval(item)) # 将字符串转换为字典
- if d['姓名'] != name:
- wfile.write(str(d)+'\n')
- else:
- flag = True
- if flag:
- print("学生{}的信息已被删除!".format(name))
- else:
- print("查无此人!")
- break
- findAll()
- answer = input("是否继续删除?y/n")
- if answer == 'y':
- continue
- else:
- break
- elif answer == "1":
- while True:
- # 通过id删除学生信息
- id = input("请输入学生id:")
- if id != ' ':
- if os.path.exists(filename):
- with open(filename, 'r', encoding='utf-8') as file:
- student_old = file.readlines()
- else:
- student_old = []
- flag = False
- if student_old: # 空列表等于 False,非空列表等于True
- with open(filename, 'w', encoding='utf-8') as wfile:
- # 只用于写入。如果该文件已存在则打开文件,并从开头开始编辑,即原有内容会被删除
- d = {}
- for item in student_old:
- d = dict(eval(item)) # 将字符串转换为字典
- if d['学号'] != id:
- wfile.write(str(d)+'\n')
- else:
- flag = True
- if flag:
- print("学号为{}的学生的信息已被删除!".format(id))
- else:
- print("查无此人!")
- break
- findAll()
- answer = input("是否继续删除?y/n")
- if answer == 'y':
- continue
- else:
- break
3、修改学生信息代码模块
- # 修改学生信息
- def revise():
- print("请问你想通过姓名修改还是学号修改学生已录入信息?")
- answer = input("按ID修改请按1,按0取消修改")
- if answer == "1":
- if os.path.exists(filename):
- with open(filename, 'r', encoding='utf-8') as file:
- student_old = file.readlines()
- else:
- return
- # 通过学号修改学生信息
- id = input("请输入学生ID:")
- with open(filename, 'r', encoding='utf-8') as file:
- students = file.readlines()
- students_new = []
- for item in students:
- d = dict(eval(item))
- students_new.append(d)
- show_query(students_new)
- for item in student_old:
- d = {}
- d = dict(eval(item))
- if d['学号'] == id:
- print("已经找到学生信息,请修改相关信息")
- while True:
- try:
- d['姓名'] = input("请输入姓名")
- d['学号'] = input("请输入学号")
- d['年龄'] = input("请输入年龄")
- d['性别'] = input("请输入性别")
- d['Python成绩'] = input("请输入Python成绩")
- d['Java成绩'] = input("请输入Java成绩")
- d['MySQL成绩'] = input("请输入MySQL成绩")
- except:
- print("输入有误,请重新输入")
- else:
- print("Sorry,查无此人!")
- break
- file.write(str(d) + '\n')
- print("修改成功")
- else:
- file.write(str(d)+'\n')
- answer = input("是否继续修改其他学生信息?y/n")
- if answer == 'y':
- revise()
- else:
- return
- elif answer == "0":
- return
4、查找学生信息代码模块
- # 查找学生信息
- def find():
- while True:
- id = ''
- name = ''
- if os.path.exists(filename):
- mode = input("按ID查找请按1,按姓名查找请按2:")
- if mode == '1':
- id = input("请输入学生ID:")
- elif mode == '2':
- name = input("请输入学生姓名:")
- else:
- print("输入有误,请重新输入")
- find()
- with open(filename, 'r', encoding='utf-8') as file:
- student = file.readlines()
- for item in student:
- d = dict(eval(item))
- if id != '':
- if d['学号'] == id:
- student_query.append(d)
- elif name != '':
- if d['姓名'] == name:
- student_query.append(d)
- show_query(student_query)
- # 清空列表
- student_query.clear()
- answer = input("是否继续查询?y/n")
- if answer == 'y':
- continue
- else:
- break
- else:
- print("无学生信息")
- return
5、查看所有学生信息代码模块
- # 查看所有学生信息
- def findAll():
- print("所有学生信息如下:")
- if os.path.exists(filename):
- with open(filename, 'r', encoding='utf-8') as file:
- student = file.readlines()
- for item in student:
- d = dict(eval(item))
- stockpile_list.append(d)
- show_query(stockpile_list)
6、排序学生成绩代码模块
- # 排列学生三科总分成绩
- def sort():
- global asc_or_desc_bool
- addAList()
- asc_or_desc = input("请选择升序 0 还是降序 1?")
- if asc_or_desc == '0':
- asc_or_desc_bool = False
- elif asc_or_desc == '1':
- asc_or_desc_bool = True
- else:
- print("输入有误,请重新输入")
- sort()
- while True:
- mode = input("请选择排序方式(1.Python成绩 2.Java成绩 3.MySQL成绩 4.总成绩):")
- if mode == '1':
- student_query.sort(key=lambda x : int(x['Python成绩']), reverse=asc_or_desc_bool)
- # x 代表列表students_new中的项
- elif mode == '2':
- student_query.sort(key=lambda x: int(x['Java成绩']), reverse=asc_or_desc_bool)
- elif mode == '3':
- student_query.sort(key=lambda x: int(x['MySQL成绩']), reverse=asc_or_desc_bool)
- elif mode == '4':
- student_query.sort(key=lambda x: int(x['Python成绩']) + int(x['Java成绩']) + int(x['MySQL成绩']), reverse=asc_or_desc_bool)
- else:
- print("输入有误,请重新输入")
- show_query(student_query)
- modeIn = input("是否选择其他排序方式(是的话请按1,否的话请按0)")
- if modeIn == '1':
- continue
- elif modeIn == '0':
- break
- else:
- print("输入有误,请重新输入")
7、统计学生总人数代码模块
- # 统计学生总人数
- def count():
- if os.path.exists(filename):
- with open(filename, 'r', encoding='utf-8') as file:
- student = file.readlines()
- for item in student:
- d = dict(eval(item))
- student_query.append(d)
- count = 0
- for i in student_query:
- count += 1
- print("该校收录了{}名学生!".format(count))
- countBoy = 0
- countGril = 0
- for j in student_query:
- if j['性别'] == "男":
- countBoy += 1
- else:
- countGril += 1
- print("该校收录了{}名男学生和{}名女学生!".format(countBoy, countGril))
- # 清空列表
- student_query.clear()
- else:
- print("无学生信息")
- return
该程序整体分为以上7大模块,最终代码呈现如下:可以收藏使用!
下一个为Java版本的星空城市管理系统。
- #!/usr/bin/env python
- # -*- coding: UTF-8 -*-
- """
- @Project :YIC_IDEA_Python
- @File :学生信息管理系统.py
- @IDE :IntelliJ IDEA
- @Author :一冲子
- @Date :2022/11/23 15:20
- """
- import os.path
-
-
- def menu():
- # 输出菜单
- print('''
- 学生信息管理系统
- =============== 功能菜单 ===============
-
- 1 新增学生信息
- 2 删除学生信息
- 3 修改学生信息
- 4 查找学生信息
- 5 查看所有学生信息
- 6 排序学生成绩
- 7 统计学生总人数
- 0 退出系统
- =========================================
- 说明:通过数字选择菜单
- ''')
-
-
- def main(): # 一直调用主菜单 让用户选择
- while True:
- menu()
- choice = int(input('请选择:'))
- if choice in [0, 1, 2, 3, 4, 5, 6, 7, 8]:
- if choice == 0: # 退出系统
- answer = input('您确定要退出系统吗y/n:')
- if answer == 'y' or answer == 'Y':
- print('感谢您的使用!!!')
- break
- elif answer !='y' or answer !='Y':
- print("不要随便按呀")
- continue
- if choice == 1: # 新增学生信息
- increase()
- if choice == 2: # 删除学生信息
- delete()
- if choice == 3: # 修改学生信息
- revise()
- if choice == 4: # 查找学生信息
- find()
- if choice == 5: # 查看所有学生信息
- findAll()
- if choice == 6: # 排列学生信息
- sort()
- if choice == 7: # 统计学生总人数
- count()
-
-
- stockpile_list = [] # 储存所有学生的数据
- student_query = []
- filename = 'students.txt'
-
-
- # 新增学生信息
- def increase():
- print("你好,先输入学号查看是否已存在学生信息...")
- id = input("请输入学生学号:")
- while True:
- if os.path.exists(filename):
- with open(filename, 'r', encoding='utf-8') as file:
- student_old = file.readlines()
- with open(filename, 'r', encoding='utf-8') as file:
- students = file.readlines()
- students_new = []
- for item in students:
- d = dict(eval(item))
- students_new.append(d)
- # show_query(students_new)
- for item in student_old:
- d = {}
- d = dict(eval(item))
- if d['学号'] == id:
- print("当前学号已存在,请重新申请学号...")
- continue
- else:
- break
- print("现在为你录入学生基本信息...")
- id = input("请输入学生id:")
- name = input("请输入学生姓名:")
- age = input("请输入学生年龄:")
- gender = input("请输入学生性别:")
- print("OK...现在开始录入成绩...")
- sub1 = int(input("请输入Python成绩:"))
- sub2 = int(input("请输入Java成绩:"))
- sub3 = int(input("请输入MySQL成绩:"))
- stockpile_list.append(
- {"姓名": name, "学号": id, "Python成绩": sub1, "年龄": age, "性别": gender, "Java成绩": sub2, "MySQL成绩": sub3})
- # 调用save函数保存信息
- save(stockpile_list)
- print("添加成功!")
- break
-
-
- # save函数保存学生信息
- def save(lst):
- try:
- stu_txt = open(filename, 'a', encoding='utf-8')
- except:
- stu_txt = open(filename, 'w', encoding='utf-8')
- for item in lst:
- stu_txt.write(str(item) + '\n')
- stu_txt.close()
-
-
- # 删除学生信息
- def delete():
- print("请问你想通过姓名删除还是学号删除?")
- answer = input("按ID查询请按1,按姓名查询请按2")
- if answer == "2":
- while True:
- # 通过姓名删除学生信息
- name = input("请输入学生姓名:")
- if name != ' ':
- if os.path.exists(filename):
- with open(filename, 'r', encoding='utf-8') as file:
- student_old = file.readlines()
- else:
- student_old = []
- flag = False
- if student_old: # 空列表等于 False,非空列表等于True
- with open(filename, 'r', encoding='utf-8') as wfile:
- # 只用于写入。如果该文件已存在则打开文件,并从开头开始编辑,即原有内容会被删除
- d = {}
- for item in student_old:
- d = dict(eval(item)) # 将字符串转换为字典
- if d['姓名'] != name:
- wfile.write(str(d)+'\n')
- else:
- flag = True
- if flag:
- print("学生{}的信息已被删除!".format(name))
- else:
- print("查无此人!")
- break
- findAll()
- answer = input("是否继续删除?y/n")
- if answer == 'y':
- continue
- else:
- break
- elif answer == "1":
- while True:
- # 通过id删除学生信息
- id = input("请输入学生id:")
- if id != ' ':
- if os.path.exists(filename):
- with open(filename, 'r', encoding='utf-8') as file:
- student_old = file.readlines()
- else:
- student_old = []
- flag = False
- if student_old: # 空列表等于 False,非空列表等于True
- with open(filename, 'w', encoding='utf-8') as wfile:
- # 只用于写入。如果该文件已存在则打开文件,并从开头开始编辑,即原有内容会被删除
- d = {}
- for item in student_old:
- d = dict(eval(item)) # 将字符串转换为字典
- if d['学号'] != id:
- wfile.write(str(d)+'\n')
- else:
- flag = True
- if flag:
- print("学号为{}的学生的信息已被删除!".format(id))
- else:
- print("查无此人!")
- break
- findAll()
- answer = input("是否继续删除?y/n")
- if answer == 'y':
- continue
- else:
- break
-
-
- # 修改学生信息
- def revise():
- print("请问你想通过姓名修改还是学号修改学生已录入信息?")
- answer = input("按ID修改请按1,按0取消修改")
- if answer == "1":
- if os.path.exists(filename):
- with open(filename, 'r', encoding='utf-8') as file:
- student_old = file.readlines()
- else:
- return
- # 通过学号修改学生信息
- id = input("请输入学生ID:")
- with open(filename, 'r', encoding='utf-8') as file:
- students = file.readlines()
- students_new = []
- for item in students:
- d = dict(eval(item))
- students_new.append(d)
- show_query(students_new)
- for item in student_old:
- d = {}
- d = dict(eval(item))
- if d['学号'] == id:
- print("已经找到学生信息,请修改相关信息")
- while True:
- try:
- d['姓名'] = input("请输入姓名")
- d['学号'] = input("请输入学号")
- d['年龄'] = input("请输入年龄")
- d['性别'] = input("请输入性别")
- d['Python成绩'] = input("请输入Python成绩")
- d['Java成绩'] = input("请输入Java成绩")
- d['MySQL成绩'] = input("请输入MySQL成绩")
- except:
- print("输入有误,请重新输入")
- else:
- print("Sorry,查无此人!")
- break
- file.write(str(d) + '\n')
- print("修改成功")
- else:
- file.write(str(d)+'\n')
- answer = input("是否继续修改其他学生信息?y/n")
- if answer == 'y':
- revise()
- else:
- return
- elif answer == "0":
- return
-
-
- # 查找学生信息
- def find():
- while True:
- id = ''
- name = ''
- if os.path.exists(filename):
- mode = input("按ID查找请按1,按姓名查找请按2:")
- if mode == '1':
- id = input("请输入学生ID:")
- elif mode == '2':
- name = input("请输入学生姓名:")
- else:
- print("输入有误,请重新输入")
- find()
- with open(filename, 'r', encoding='utf-8') as file:
- student = file.readlines()
- for item in student:
- d = dict(eval(item))
- if id != '':
- if d['学号'] == id:
- student_query.append(d)
- elif name != '':
- if d['姓名'] == name:
- student_query.append(d)
- show_query(student_query)
- # 清空列表
- student_query.clear()
- answer = input("是否继续查询?y/n")
- if answer == 'y':
- continue
- else:
- break
- else:
- print("无学生信息")
- return
-
-
- # 内容呈现
- def show_query(lst):
- if len(lst) == 0:
- print("无相关信息")
- return
- # 定义标题显示格式
- format_title = '{:^7}\t{:^7}\t{:^7}\t{:^7}\t{:^7}\t{:^7}\t{:^7}\t{:^7}'
- print(format_title.format('学号', '姓名', '年龄', '性别', 'Python成绩', 'Java成绩', 'MySQL成绩', '总成绩'))
- # 定义内容显示格式
- format_data = '{:^9}\t{:^7}\t{:^8}\t{:^8}\t{:^7}\t{:^12}\t{:^9}\t{:^12}'
- for item in lst:
- print(format_data.format(item.get('学号'),
- item.get('姓名'),
- item.get('年龄'),
- item.get('性别'),
- item.get('Python成绩'),
- item.get('Java成绩'),
- item.get('MySQL成绩'),
- int( item.get('Python成绩')) + int( item.get('Java成绩')) + int( item.get('MySQL成绩'))
- ))
-
-
- # 查看所有学生信息
- def findAll():
- print("所有学生信息如下:")
- if os.path.exists(filename):
- with open(filename, 'r', encoding='utf-8') as file:
- student = file.readlines()
- for item in student:
- d = dict(eval(item))
- stockpile_list.append(d)
- show_query(stockpile_list)
- # for i in stockpile_list:
- # print("{}同学,学号:{},性别:{},今年{}岁,三科成绩如下:Python:{}分、Java:{}分、MySQL:{}分"\
- # .format(i['姓名'], i['学号'], i['性别'], i['年龄'], i['Python成绩'], i['Java成绩'], i['MySQL成绩']))
-
-
- # 排列学生三科总分成绩
- def sort():
- global asc_or_desc_bool
- addAList()
- asc_or_desc = input("请选择升序 0 还是降序 1?")
- if asc_or_desc == '0':
- asc_or_desc_bool = False
- elif asc_or_desc == '1':
- asc_or_desc_bool = True
- else:
- print("输入有误,请重新输入")
- sort()
- while True:
- mode = input("请选择排序方式(1.Python成绩 2.Java成绩 3.MySQL成绩 4.总成绩):")
- if mode == '1':
- student_query.sort(key=lambda x : int(x['Python成绩']), reverse=asc_or_desc_bool)
- # x 代表列表students_new中的项
- elif mode == '2':
- student_query.sort(key=lambda x: int(x['Java成绩']), reverse=asc_or_desc_bool)
- elif mode == '3':
- student_query.sort(key=lambda x: int(x['MySQL成绩']), reverse=asc_or_desc_bool)
- elif mode == '4':
- student_query.sort(key=lambda x: int(x['Python成绩']) + int(x['Java成绩']) + int(x['MySQL成绩']), reverse=asc_or_desc_bool)
- else:
- print("输入有误,请重新输入")
- show_query(student_query)
- modeIn = input("是否选择其他排序方式(是的话请按1,否的话请按0)")
- if modeIn == '1':
- continue
- elif modeIn == '0':
- break
- else:
- print("输入有误,请重新输入")
-
-
- # 统计学生总人数
- def count():
- if os.path.exists(filename):
- with open(filename, 'r', encoding='utf-8') as file:
- student = file.readlines()
- for item in student:
- d = dict(eval(item))
- student_query.append(d)
- count = 0
- for i in student_query:
- count += 1
- print("该校收录了{}名学生!".format(count))
- countBoy = 0
- countGril = 0
- for j in student_query:
- if j['性别'] == "男":
- countBoy += 1
- else:
- countGril += 1
- print("该校收录了{}名男学生和{}名女学生!".format(countBoy, countGril))
- # 清空列表
- student_query.clear()
- else:
- print("无学生信息")
- return
-
-
- # 将学生信息添加到列表
- def addAList():
- if os.path.exists(filename):
- with open(filename, 'r', encoding='utf-8') as file:
- student = file.readlines()
- for item in student:
- d = dict(eval(item))
- student_query.append(d)
-
-
- # 只有语句运行main的时候 这个输出语句才会运、不加的则其他中调用此模块函数 也会输出
- if __name__ == '__main__':
- main()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。