当前位置:   article > 正文

Python 实现学生成绩管理系统_python用字典创建一个学生成绩管理系统

python用字典创建一个学生成绩管理系统
  1. # 源码
  2. import os
  3. filename = 'student.txt'
  4. def main():
  5. while True:
  6. menum()
  7. choice = int(input('请选择:'))
  8. if choice in [0, 1, 2, 3, 4, 5, 6, 7]:
  9. if choice == 0:
  10. answer = input('您确定要退出系统吗? y/n')
  11. if answer == 'y' or answer == 'Y':
  12. print('谢谢你的使用!')
  13. break # 退出系统
  14. else:
  15. continue
  16. elif choice == 1:
  17. insert() # 录入学生信息
  18. elif choice == 2:
  19. search()
  20. elif choice == 3:
  21. delete()
  22. elif choice == 4:
  23. modify()
  24. elif choice == 5:
  25. sort()
  26. elif choice == 6:
  27. total()
  28. elif choice == 7:
  29. show()
  30. else:
  31. print('输入错误,请重新输入')
  32. def menum():
  33. print('==================学生信息管理系统===================')
  34. print('---------------------功能菜单-------------------------')
  35. print('\t\t\t\t1、录入学生信息')
  36. print('\t\t\t\t2、查找学生信息')
  37. print('\t\t\t\t3、删除学生信息')
  38. print('\t\t\t\t4、修改学生信息')
  39. print('\t\t\t\t5、排序')
  40. print('\t\t\t\t6、统计学生总人数')
  41. print('\t\t\t\t7、显示所有学生信息')
  42. print('\t\t\t\t0、推出系统')
  43. print('-----------------------------------------------------')
  44. def insert():
  45. student_list = []
  46. while True:
  47. id = input('请输入ID(如10001):')
  48. if not id:
  49. break
  50. name = input('请输入姓名:')
  51. if not name:
  52. break
  53. try:
  54. English = int(input('请输入英语成绩:'))
  55. Python = int(input('请输入Python成绩:'))
  56. Java = int(input('请输入Java成绩:'))
  57. except:
  58. print('输入无效,不是整数类型,请重新输入')
  59. continue
  60. # 将录入的学生信息保存到字典当中
  61. student = {'id': id, 'name': name, 'English': English, 'Python': Python, 'Java': Java}
  62. # 将学生信息添加到列表中
  63. student_list.append(student)
  64. answer = input('是否继续添加?y/n\n')
  65. if answer == 'y' or answer == 'Y':
  66. continue
  67. else:
  68. break
  69. # 调用save()函数
  70. save(student_list)
  71. print('学生信息录入完毕!')
  72. def save(lst):
  73. try:
  74. stu_txt = open(filename, 'a+', encoding='utf-8')
  75. except:
  76. stu_txt = open(filename, 'w', encoding='utf-8')
  77. for item in lst:
  78. stu_txt.write(str(item) + '\n')
  79. stu_txt.close()
  80. def search():
  81. student_query = []
  82. while True:
  83. id = ''
  84. name = ''
  85. if os.path.exists(filename):
  86. mode = input('按ID查找请输入1,按姓名查找请输入2:')
  87. if mode == '1':
  88. id = input('请输入你想要查找的学生ID:')
  89. elif mode == '2':
  90. name = input('请输入你想要查找的学生姓名:')
  91. else:
  92. print('您的输入有误,请重新输入!')
  93. search()
  94. with open(filename, 'r', encoding='utf-8') as rfile:
  95. student_old = rfile.readlines()
  96. for item in student_old:
  97. d = dict(eval(item))
  98. if id != '':
  99. if d['id'] == id:
  100. student_query.append(d)
  101. elif name != '':
  102. if d['name'] == name:
  103. student_query.append(d)
  104. # 显示查询结果
  105. show_student(student_query)
  106. # 清空列表
  107. student_query.clear()
  108. answer = input('您要继续查找学生信息吗?y/n')
  109. if answer == 'y' or answer == 'Y':
  110. continue
  111. else:
  112. break
  113. else:
  114. print('暂未保存学生信息!')
  115. return
  116. def show_student(lst):
  117. if len(lst) == 0:
  118. print('没有查询到学生信息,无数据显示!')
  119. return
  120. # 定义标题显示格式
  121. format_title = '{:^6}\t{:^12}\t{:^8}\t{:^10}\t{:^10}\t{:^8}'
  122. print(format_title.format('ID', '姓名', '英语成绩', 'Python成绩', 'Java成绩', '总成绩'))
  123. # 定义内容的显示格式
  124. format_data = '{:^6}\t{:^12}\t{:^8}\t{:^10}\t{:^10}\t{:^8}'
  125. for item in lst:
  126. print(format_data.format(item['id'],
  127. item['name'],
  128. item['English'],
  129. item['Python'],
  130. item['Java'],
  131. int(item['English']) + int(item['Python']) + int(item['Java'])))
  132. def delete():
  133. while True:
  134. student_id = input('请输入要删除的学生的ID:')
  135. if student_id != '':
  136. if os.path.exists(filename):
  137. with open(filename, 'r', encoding='utf-8') as file:
  138. student_old = file.readlines()
  139. else:
  140. student_old = []
  141. flag = False # 标记是否删除
  142. if student_old:
  143. with open(filename, 'w', encoding='utf-8') as wfile:
  144. d = {}
  145. for item in student_old:
  146. d = dict(eval(item)) # 将字符串转成字典
  147. if d['id'] != student_id:
  148. wfile.write(str(d) + '\n')
  149. else:
  150. flag = True
  151. if flag:
  152. print(f'ID为{student_id}的学生信息已经被删除')
  153. else:
  154. print(f'没有找到ID为{student_id}的学生信息')
  155. else:
  156. print('无学生信息')
  157. break
  158. show() # 删除之后要重新显示所有学生信息
  159. answer = input('是否继续删除?y/n\n')
  160. if answer == 'y' or answer == 'Y':
  161. continue
  162. else:
  163. break
  164. def modify():
  165. show()
  166. if os.path.exists(filename):
  167. with open(filename, 'r', encoding='utf-8') as rfile:
  168. student_old = rfile.readlines()
  169. else:
  170. return
  171. student_id = input('请输入需要修改的学生ID')
  172. if student_old:
  173. with open(filename, 'w', encoding='utf-8') as wfile:
  174. d = {}
  175. for item in student_old:
  176. d = dict(eval(item))
  177. if d['id'] == student_id:
  178. print('找到学生信息,可以修改他的相关信息!')
  179. while True:
  180. try:
  181. d['name'] = input('请输入姓名:')
  182. d['English'] = input('请输入英语成绩:')
  183. d['Java'] = input('请输入Java成绩:')
  184. d['Python'] = input('请输入Python成绩:')
  185. except:
  186. print('您的输入有误!,请重新输入!!')
  187. else:
  188. break
  189. wfile.write(str(d) + '\n')
  190. print('修改成功!')
  191. else:
  192. wfile.write(str(d) + '\n')
  193. answer = input('是否继续修改?y/n\n')
  194. if answer == 'y' or answer == 'Y':
  195. modify()
  196. def sort():
  197. show()
  198. if os.path.exists(filename):
  199. with open(filename, 'r', encoding='utf-8') as rfile:
  200. student_old = rfile.readlines()
  201. student_new = []
  202. for item in student_old:
  203. student_new.append(eval(item))
  204. else:
  205. print('暂无学生信息!!')
  206. return
  207. asc_or_desc = input('请选择(0.升序 1.降序):')
  208. if asc_or_desc == '0':
  209. asc_or_desc = False
  210. elif asc_or_desc == '1':
  211. asc_or_desc = True
  212. else:
  213. print('输入有误!请重新输入')
  214. sort()
  215. mode = input('请选择排序方式(1.按英语成绩排序 2.按Python成绩排序 3.按Java成绩排序 4.按总成绩排序):')
  216. if mode == '1':
  217. student_new.sort(key=lambda x: int(x['English']), reverse=asc_or_desc) # 排序
  218. elif mode == '2':
  219. student_new.sort(key=lambda x: int(x['Python']), reverse=asc_or_desc)
  220. elif mode == '3':
  221. student_new.sort(key=lambda x: int(x['Java']), reverse=asc_or_desc)
  222. elif mode == '4':
  223. student_new.sort(key=lambda x: int(x['English'])+int(x['Python'])+int(x['Java']))
  224. else:
  225. print('输入有误,请重新输入!')
  226. sort()
  227. show_student(student_new)
  228. def total():
  229. if os.path.exists(filename):
  230. with open(filename, 'r', encoding='utf-8') as rfile:
  231. student_old = rfile.readlines()
  232. if student_old:
  233. print('一共有{0}名学生'.format(len(student_old)))
  234. else:
  235. print('还没有录入学生信息!')
  236. else:
  237. print('无学生信息!')
  238. def show():
  239. student_lst = []
  240. if os.path.exists(filename):
  241. with open(filename, 'r', encoding='utf-8') as rfile:
  242. student_old = rfile.readlines()
  243. for item in student_old:
  244. student_lst.append(eval(item))
  245. if student_lst:
  246. show_student(student_lst)
  247. else:
  248. print('暂无学生信息!')
  249. if __name__ == '__main__':
  250. main()
  251. """
  252. 文件打包成.exe
  253. pip install PyInstaller
  254. pyinstaller -F 绝对路径
  255. """
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/218356
推荐阅读
相关标签
  

闽ICP备14008679号