当前位置:   article > 正文

用Python实现学生成绩管理系统,附上完整代码_python学生成绩管理系统

python学生成绩管理系统

大体功能如下,用Python语言完成的小项目,功能包括录入、查找、删除、修改、排序、显示等,提示内容有特色~可以自己修改

代码附上:

  1. '''
  2. 作者:淳i璐
  3. 项目:学生成绩管理系统
  4. '''
  5. import os
  6. filename = 'student.txt'
  7. def main():
  8. while True:
  9. menu()
  10. choice = int(input('选一个来来来:'))
  11. if choice in [0,1,2,3,4,5,6,7]:
  12. if choice == 0:
  13. choice1 = input('主人你舍得退出系统吗^_^ (T/F):')
  14. if choice1 == 'T' or choice1 == 't':
  15. break
  16. else:
  17. continue
  18. elif choice == 1:
  19. insert()
  20. elif choice == 2:
  21. search()
  22. elif choice == 3:
  23. delete()
  24. elif choice == 4:
  25. modify()
  26. elif choice == 5:
  27. sort()
  28. elif choice == 6:
  29. total()
  30. elif choice == 7:
  31. show()
  32. else:
  33. print('输入有误,麻溜重新输入:')
  34. print('可恶啊,让你跑了!')
  35. def insert():
  36. student_list = []
  37. while True:
  38. id = input('学生学号交上来(eg:1001):')
  39. if not id:
  40. break
  41. name = input('尊姓大名???:')
  42. if not name:
  43. break
  44. try:
  45. english = int(input('敢问阁下英语成绩?:'))
  46. chinese = int(input('敢问阁下语文成绩?:'))
  47. java = int(input('敢问阁下java成绩?:'))
  48. print('okok真棒!')
  49. except:
  50. print('请你好好输入哈,警告!')
  51. continue
  52. student = {'id':id,'name':name,'english':english,'chinese':chinese,'java':java}
  53. student_list.append(student)
  54. choice2 = input('还继续吗bro?(T/F):')
  55. if choice2 == 't' or choice2 == 'T':
  56. continue
  57. else:
  58. break
  59. save(student_list)
  60. def save(lst):
  61. try:
  62. stu_txt = open(filename,'a',encoding='utf-8')
  63. except:
  64. stu_txt = open(filename,'w',encoding='utf-8')
  65. for i in lst:
  66. stu_txt.write(str(i)+'\n')
  67. stu_txt.close()
  68. print('* 嘻嘻,录入完毕!*')
  69. def search():
  70. student_search = []
  71. while True:
  72. id = ''
  73. name = ''
  74. if os.path.exists(filename):
  75. choice = input('按id查输入1,按姓名查输入2:')
  76. if choice == '1':
  77. id = input('id啥了')
  78. elif choice == '2':
  79. name = input('大名啥了')
  80. else:
  81. print('搞错了,重来')
  82. search()
  83. with (open(filename,'r',encoding='utf-8') as rfile):
  84. student = rfile.readlines()
  85. for i in student:
  86. d = dict(eval(i))
  87. if id != '':
  88. if d['id'] == id:
  89. student_search.append(d)
  90. elif name != '':
  91. if d['name'] == name:
  92. student_search.append(d)
  93. else:
  94. print('输入呀')
  95. show_student(student_search)
  96. student_search.clear()
  97. answer = input('是否继续查询(y/n)')
  98. if answer == 'y' or answer == 'Y':
  99. continue
  100. else:
  101. break
  102. else:
  103. print('没有学生信息')
  104. return
  105. def show_student(lst):
  106. if len(lst) == 0:
  107. print('无数据')
  108. return
  109. #标题显示
  110. print('----show time -------')
  111. format_title = '{:^6}\t{:^12}\t{:^8}\t{:^10}\t{:^10}\t{:^8}'
  112. print(format_title.format('ID','姓名','英语','语文','Java','总成绩'))
  113. #内容显示
  114. format_data = '{:^6}\t{:^12}\t{:^8}\t{:^10}\t{:^10}\t{:^8}'
  115. for item in lst:
  116. print(format_data.format(item.get('id'),
  117. item.get('name'),
  118. item.get('english'),
  119. item.get('chinese'),
  120. item.get('java'),
  121. int(item.get('english'))+int(item.get('chinese'))+int(item.get('java'))
  122. ))
  123. def delete():
  124. while True:
  125. student_id = input('你要删的学生的id:')
  126. if student_id != '':
  127. if os.path.exists(filename):
  128. with open(filename,'r',encoding='utf-8') as file:
  129. student_old = file.readlines()
  130. else:
  131. student_old = []
  132. flag = False
  133. if student_old:
  134. with open(filename,'w',encoding='utf-8') as wfile:
  135. d = {}
  136. for i in student_old:
  137. d = dict(eval(i)) #字符串转为字典
  138. if d['id'] != student_id:
  139. wfile.write(str(d)+'\n')
  140. else:
  141. flag = True
  142. if flag:
  143. print(f'id为{student_id}的学生已经被删除')
  144. else:
  145. print(f'id为{student_id}的学生他也煤油啊!')
  146. else:
  147. print('不存在学生')
  148. break
  149. show()
  150. anwser = input('你还删吗??!(T/F)')
  151. if anwser == 't' or anwser == 'T':
  152. continue
  153. else:
  154. break
  155. def modify():
  156. show()
  157. if os.path.exists(filename):
  158. with open(filename, 'r', encoding='utf-8') as file:
  159. student_old = file.readlines()
  160. else:
  161. student_old = []
  162. while True:
  163. flag = False
  164. student_id = input('请输入你要修改滴学生的ID:')
  165. with open(filename, 'w', encoding='utf-8') as wfile:
  166. d = {}
  167. for i in student_old:
  168. d = dict(eval(i)) # 字符串转为字典
  169. if d['id'] == student_id:
  170. print('---找到这个学生了!改他!---')
  171. student_name = input('叫啥呀?')
  172. d['name'] = student_name
  173. student_english = input('英语几分?')
  174. d['english'] = student_english
  175. student_chinese = input('语文几分?')
  176. d['chinese'] = student_chinese
  177. student_java = input('java几分?')
  178. d['java'] = student_java
  179. wfile.write(str(d)+'\n')
  180. flag = True
  181. else:
  182. wfile.write(str(d)+'\n')
  183. if flag == True:
  184. print(f'id为{student_id}的学生已经修改')
  185. else:
  186. print('没找到他啊')
  187. show()
  188. answer = input('还改吗?(t/f)')
  189. if answer == 't' or answer == 'T':
  190. continue
  191. else:
  192. break
  193. def sort():
  194. show()
  195. if os.path.exists(filename):
  196. stu_show = []
  197. with open(filename,'r',encoding='utf-8') as rfile:
  198. stu = rfile.readlines()
  199. for i in stu:
  200. d = dict(eval(i))
  201. stu_show.append(d)
  202. else:
  203. print('没学生咋排序!')
  204. return
  205. choice = input('升序1,降序2:')
  206. if choice == '1':
  207. stu_bool = False
  208. elif choice == '2':
  209. stu_bool = True
  210. else:
  211. sort()
  212. mode = input('---0总成绩,1英语,2语文,3Java:')
  213. if mode == '0':
  214. stu_show.sort(key=lambda x:int(x['english']) + int(x['chinese']) + int(x['java']),reverse=stu_bool)
  215. elif mode == '1':
  216. stu_show.sort(key=lambda x: int(x['english']), reverse=stu_bool)
  217. elif mode == '2':
  218. stu_show.sort(key=lambda x: int(x['chinese']), reverse=stu_bool)
  219. elif mode == '3':
  220. stu_show.sort(key=lambda x: int(x['java']), reverse=stu_bool)
  221. else:
  222. sort()
  223. show_student(stu_show)
  224. answer = input('还排吗 t/f:')
  225. if answer == 't':
  226. sort()
  227. else:
  228. return
  229. def total():
  230. if os.path.exists(filename):
  231. with open(filename, 'r', encoding='utf-8') as rfile:
  232. student = rfile.readlines()
  233. if student:
  234. print(f'一共有{len(student)}银')
  235. else:
  236. print('无信息')
  237. else:
  238. print('无信息')
  239. return
  240. def show():
  241. stu_show = []
  242. if os.path.exists(filename):
  243. with open(filename,'r',encoding='utf-8') as rfile:
  244. stu = rfile.readlines()
  245. for i in stu:
  246. d = dict(eval(i))
  247. stu_show.append(d)
  248. if stu_show:
  249. show_student(stu_show)
  250. else:
  251. print('不存在')
  252. else:
  253. print('学生都跑光了')
  254. def menu():
  255. print('--------------学生成绩管理系统-------------')
  256. print('--------------== 功能菜单 ==-------------')
  257. print('\t\t\t\t\t1.录入学生信息')
  258. print('\t\t\t\t\t2.查找学生信息')
  259. print('\t\t\t\t\t3.删除学生信息')
  260. print('\t\t\t\t\t4.修改学生信息')
  261. print('\t\t\t\t\t5.排序')
  262. print('\t\t\t\t\t6.统计学生人数')
  263. print('\t\t\t\t\t7.显示所有学生信息')
  264. print('\t\t\t\t\t0.退出')
  265. print('--------------------------------------')
  266. if __name__ == '__main__':
  267. main()

简简单单,完成了入门Python的第一个小小项目,未来可期!

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号