Python做一個生成txt的學生成績錄入


本人小白一枚

 1 #/usr/bin/env python
 2 #-*- coding:utf-8 -*-
 3 # @Time : 2020/7/25 0:11 
 4 # @Author : Zero, 紛飛的花火丶
 5 # @File : 學生管理系統.py 
 6 # @Software: PyCharm
 7 
 8 ls = ['排名\t姓名\t語文\t數學\t英語\t總分\n']
 9 score = []
10 n = 3
11 print("歡迎來到學生錄入系統")
12 
13 # 學生成績填入
14 def filewrite():
15     names = input("請輸入姓名:")
16     scoreCN = input("請輸入語文成績:")
17     scoreMH = input("請輸入數學成績:")
18     scoreEN = input("請輸入英語成績:")
19     ls.append(f"{names}\t{scoreCN}\t{scoreMH}\t{scoreEN}\n")
20 
21 filewrite()
22 
23 while True:
24     ct = input("是否繼續錄入(y/n):")
25     if ct in ['Yes', 'YES', 'Y', 'y']:
26         filewrite()
27 
28     elif ct in ['No', 'NO', 'N', 'n']:
29         for i in ls:
30             score.append(i.split())
31         # 計算各個學生的總分並將總分加入列表
32         for h in score[1:]:
33             zf = int(h[1]) + int(h[2]) + int(h[3])
34             h.append(str(zf))
35 
36         # 按總分進行排序
37         score.sort(key = lambda x:x[4], reverse=True)
38 
39         # 給學生加入排名
40         for i in range(1, len(score[1:])+1):
41             score[i].insert(0, i)
42 
43         # 把數據寫入記事本里
44         with open(r"E:\Build\學生成績.txt",'x') as file:
45             for h in score:
46                 for i in h:
47                     file.write(str(i) + "\t")
48                 file.write("\n")
49         print("錄入完畢")
50         break
51 
52     # 用戶亂輸入三次就退出
53     else:
54         n -= 1
55         if n == 0:
56             break
57         print(f"輸入不合法,你還有{n}次機會,請重新輸入")
58         continue

 


免責聲明!

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



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