『Python3-學(xué)生信息管理系統(tǒng)』shell運(yùn)行

import re,os

filename ="students.txt"? # 定義保存學(xué)生信息的文件名

def menu():

# 輸出菜單

? ? print('''

╔———————學(xué)生信息管理系統(tǒng)————————╗

│? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? │

│? =============== 功能菜單 ===============? │

│? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? │

│? 1 錄入學(xué)生信息? ? ? ? ? ? ? ? ? ? ? ? ? ? │

│? 2 查找學(xué)生信息? ? ? ? ? ? ? ? ? ? ? ? ? ? │

│? 3 刪除學(xué)生信息? ? ? ? ? ? ? ? ? ? ? ? ? ? │

│? 4 修改學(xué)生信息? ? ? ? ? ? ? ? ? ? ? ? ? ? │

│? 5 排序? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? │

│? 6 統(tǒng)計(jì)學(xué)生總?cè)藬?shù)? ? ? ? ? ? ? ? ? ? ? ? ? │

│? 7 顯示所有學(xué)生信息? ? ? ? ? ? ? ? ? ? ? ? │

│? 0 退出系統(tǒng)? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? │

│? ==========================================? │

│? 說明:通過數(shù)字或↑↓方向鍵選擇菜單? ? ? ? ? │

╚———————————————————————╝

? ? ''')

def main():

ctrl =True? # 標(biāo)記是否退出系統(tǒng)

? ? while (ctrl):

menu()# 顯示菜單

? ? ? ? option =input("請選擇:")# 選擇菜單項(xiàng)

? ? ? ? option_str = re.sub("\D","", option)# 提取數(shù)字

? ? ? ? if option_strin ['0','1','2','3','4','5','6','7']:

option_int =int(option_str)

if option_int ==0:# 退出系統(tǒng)

? ? ? ? ? ? ? ? print('您已退出學(xué)生成績管理系統(tǒng)敢订!')

ctrl =False

? ? ? ? ? ? elif option_int ==1:# 錄入學(xué)生成績信息

? ? ? ? ? ? ? ? insert()

elif option_int ==2:# 查找學(xué)生成績信息

? ? ? ? ? ? ? ? search()

elif option_int ==3:# 刪除學(xué)生成績信息

? ? ? ? ? ? ? ? delete()

elif option_int ==4:# 修改學(xué)生成績信息

? ? ? ? ? ? ? ? modify()

elif option_int ==5:# 排序

? ? ? ? ? ? ? ? sort()

elif option_int ==6:# 統(tǒng)計(jì)學(xué)生總數(shù)

? ? ? ? ? ? ? ? total()

elif option_int ==7:# 顯示所有學(xué)生信息

? ? ? ? ? ? ? ? show()

'''1 錄入學(xué)生信息'''

def insert():

stdentList = []# 保存學(xué)生信息的列表

? ? mark =True? # 是否繼續(xù)添加

? ? while mark:

id =input("請輸入ID(如 1001):")

if not id:# ID為空岂却,跳出循環(huán)

? ? ? ? ? ? break

? ? ? ? name =input("請輸入名字:")

if not name:# 名字為空硫狞,跳出循環(huán)

? ? ? ? ? ? break

? ? ? ? try:

english =int(input("請輸入英語成績:"))

python =int(input("請輸入Python成績:"))

c =int(input("請輸入C語言成績:"))

except:

print("輸入無效,不是整型數(shù)值....重新錄入信息")

continue

? ? ? ? stdent = {"id": id,"name": name,"english": english,"python": python,"c": c}# 將輸入的學(xué)生信息保存到字典

? ? ? ? stdentList.append(stdent)# 將學(xué)生字典添加到列表中

? ? ? ? inputMark =input("是否繼續(xù)添加九串?(y/n):")

if inputMark =="y":# 繼續(xù)添加

? ? ? ? ? ? mark =True

? ? ? ? else:# 不繼續(xù)添加

? ? ? ? ? ? mark =False

? ? save(stdentList)# 將學(xué)生信息保存到文件

? ? print("學(xué)生信息錄入完畢7摺A渍恕!")

# 將學(xué)生信息保存到文件

def save(student):

try:

students_txt =open(filename,"a")# 以追加模式打開

? ? except Exception as e:

students_txt =open(filename,"w")# 文件不存在贾虽,創(chuàng)建文件并打開

? ? for infoin student:

students_txt.write(str(info) +"\n")# 按行存儲逃糟,添加換行符

? ? students_txt.close()# 關(guān)閉文件

'''2 查找學(xué)生成績信息'''

def search():

mark =True

? ? student_query = []# 保存查詢結(jié)果的學(xué)生列表

? ? while mark:

id =""

? ? ? ? name =""

? ? ? ? if os.path.exists(filename):# 判斷文件是否存在

? ? ? ? ? ? mode =input("按ID查輸入1;按姓名查輸入2:")

if mode =="1":

id =input("請輸入學(xué)生ID:")

elif mode =="2":

name =input("請輸入學(xué)生姓名:")

else:

print("您的輸入有誤蓬豁,請重新輸入绰咽!")

search()# 重新查詢

? ? ? ? ? ? with open(filename,'r')as file:# 打開文件

? ? ? ? ? ? ? ? student = file.readlines()# 讀取全部內(nèi)容

? ? ? ? ? ? ? ? for listin student:

d =dict(eval(list))# 字符串轉(zhuǎn)字典

? ? ? ? ? ? ? ? ? ? if idis not "":# 判斷是否按ID查

? ? ? ? ? ? ? ? ? ? ? ? if d['id'] == id:

student_query.append(d)# 將找到的學(xué)生信息保存到列表中

? ? ? ? ? ? ? ? ? ? elif nameis not "":# 判斷是否按姓名查

? ? ? ? ? ? ? ? ? ? ? ? if d['name'] == name:

student_query.append(d)# 將找到的學(xué)生信息保存到列表中

? ? ? ? ? ? ? ? show_student(student_query)# 顯示查詢結(jié)果

? ? ? ? ? ? ? ? student_query.clear()# 清空列表

? ? ? ? ? ? ? ? inputMark =input("是否繼續(xù)查詢?(y/n):")

if inputMark =="y":

mark =True

? ? ? ? ? ? ? ? else:

mark =False

? ? ? ? else:

print("暫未保存數(shù)據(jù)信息...")

return

'''3 刪除學(xué)生成績信息'''

def delete():

mark =True? # 標(biāo)記是否循環(huán)

? ? while mark:

studentId =input("請輸入要?jiǎng)h除的學(xué)生ID:")

if studentIdis not "":# 判斷要?jiǎng)h除的學(xué)生是否存在

? ? ? ? ? ? if os.path.exists(filename):# 判斷文件是否存在

? ? ? ? ? ? ? ? with open(filename,'r')as rfile:# 打開文件

? ? ? ? ? ? ? ? ? ? student_old = rfile.readlines()# 讀取全部內(nèi)容

? ? ? ? ? ? else:

student_old = []

ifdel =False? # 標(biāo)記是否刪除

? ? ? ? ? ? if student_old:# 如果存在學(xué)生信息

? ? ? ? ? ? ? ? with open(filename,'w')as wfile:# 以寫方式打開文件

? ? ? ? ? ? ? ? ? ? d = {}# 定義空字典

? ? ? ? ? ? ? ? ? ? for listin student_old:

d =dict(eval(list))# 字符串轉(zhuǎn)字典

? ? ? ? ? ? ? ? ? ? ? ? if d['id'] != studentId:

wfile.write(str(d) +"\n")# 將一條學(xué)生信息寫入文件

? ? ? ? ? ? ? ? ? ? ? ? else:

ifdel =True? # 標(biāo)記已經(jīng)刪除

? ? ? ? ? ? ? ? ? ? if ifdel:

print("ID為 %s 的學(xué)生信息已經(jīng)被刪除..." % studentId)

else:

print("沒有找到ID為 %s 的學(xué)生信息..." % studentId)

else:# 不存在學(xué)生信息

? ? ? ? ? ? ? ? print("無學(xué)生信息...")

break? # 退出循環(huán)

? ? ? ? ? ? show()# 顯示全部學(xué)生信息

? ? ? ? ? ? inputMark =input("是否繼續(xù)刪除地粪?(y/n):")

if inputMark =="y":

mark =True? # 繼續(xù)刪除

? ? ? ? ? ? else:

mark =False? # 退出刪除學(xué)生信息功能

'''4 修改學(xué)生成績信息'''

def modify():

show()# 顯示全部學(xué)生信息

? ? if os.path.exists(filename):# 判斷文件是否存在

? ? ? ? with open(filename,'r')as rfile:# 打開文件

? ? ? ? ? ? student_old = rfile.readlines()# 讀取全部內(nèi)容

? ? else:

return

? ? studentid =input("請輸入要修改的學(xué)生ID:")

with open(filename,"w")as wfile:# 以寫模式打開文件

? ? ? ? for studentin student_old:

d =dict(eval(student))# 字符串轉(zhuǎn)字典

? ? ? ? ? ? if d["id"] == studentid:# 是否為要修改的學(xué)生

? ? ? ? ? ? ? ? print("找到了這名學(xué)生取募,可以修改他的信息!")

while True:# 輸入要修改的信息

? ? ? ? ? ? ? ? ? ? try:

d["name"] =input("請輸入姓名:")

d["english"] =int(input("請輸入英語成績:"))

d["python"] =int(input("請輸入Python成績:"))

d["c"] =int(input("請輸入C語言成績:"))

except:

print("您的輸入有誤蟆技,請重新輸入玩敏。")

else:

break? # 跳出循環(huán)

? ? ? ? ? ? ? ? student =str(d)# 將字典轉(zhuǎn)換為字符串

? ? ? ? ? ? ? ? wfile.write(student +"\n")# 將修改的信息寫入到文件

? ? ? ? ? ? ? ? print("修改成功!")

else:

wfile.write(student)# 將未修改的信息寫入到文件

? ? mark =input("是否繼續(xù)修改其他學(xué)生信息质礼?(y/n):")

if mark =="y":

modify()# 重新執(zhí)行修改操作

'''5 排序'''

def sort():

show()# 顯示全部學(xué)生信息

? ? if os.path.exists(filename):# 判斷文件是否存在

? ? ? ? with open(filename,'r')as file:# 打開文件

? ? ? ? ? ? student_old = file.readlines()# 讀取全部內(nèi)容

? ? ? ? ? ? student_new = []

for listin student_old:

d =dict(eval(list))# 字符串轉(zhuǎn)字典

? ? ? ? ? ? student_new.append(d)# 將轉(zhuǎn)換后的字典添加到列表中

? ? else:

return

? ? ascORdesc =input("請選擇(0升序旺聚;1降序):")

if ascORdesc =="0":# 按升序排序

? ? ? ? ascORdescBool =False? ? ? ? ? # 標(biāo)記變量,為False表示升序排序

? ? elif ascORdesc =="1":# 按降序排序

? ? ? ? ascORdescBool =True? ? ? ? ? # 標(biāo)記變量眶蕉,為True表示降序排序

? ? else:

print("您的輸入有誤砰粹,請重新輸入!")

sort()

mode =input("請選擇排序方式(1按英語成績排序造挽;2按Python成績排序碱璃;3按C語言成績排序;0按總成績排序):")

if mode =="1":# 按英語成績排序

? ? ? ? student_new.sort(key=lambda x: x["english"],reverse=ascORdescBool)

elif mode =="2":# 按Python成績排序

? ? ? ? student_new.sort(key=lambda x: x["python"],reverse=ascORdescBool)

elif mode =="3":# 按C語言成績排序

? ? ? ? student_new.sort(key=lambda x: x["c"],reverse=ascORdescBool)

elif mode =="0":# 按總成績排序

? ? ? ? student_new.sort(key=lambda x: x["english"] + x["python"] + x["c"],reverse=ascORdescBool)

else:

print("您的輸入有誤饭入,請重新輸入嵌器!")

sort()

show_student(student_new)# 顯示排序結(jié)果

''' 6 統(tǒng)計(jì)學(xué)生總數(shù)'''

def total():

if os.path.exists(filename):# 判斷文件是否存在

? ? ? ? with open(filename,'r')as rfile:# 打開文件

? ? ? ? ? ? student_old = rfile.readlines()# 讀取全部內(nèi)容

? ? ? ? ? ? if student_old:

print("一共有 %d 名學(xué)生!" %len(student_old))

else:

print("還沒有錄入學(xué)生信息谐丢!")

else:

print("暫未保存數(shù)據(jù)信息...")

''' 7 顯示所有學(xué)生信息 '''

def show():

student_new = []

if os.path.exists(filename):# 判斷文件是否存在

? ? ? ? with open(filename,'r')as rfile:# 打開文件

? ? ? ? ? ? student_old = rfile.readlines()# 讀取全部內(nèi)容

? ? ? ? for listin student_old:

student_new.append(eval(list))# 將找到的學(xué)生信息保存到列表中

? ? ? ? if student_new:

show_student(student_new)

else:

print("暫未保存數(shù)據(jù)信息...")

# 將保存在列表中的學(xué)生信息顯示出來

def show_student(studentList):

if not studentList:

print("(o@.@o) 無數(shù)據(jù)信息 (o@.@o) \n")

return

? ? format_title ="{:^6}{:^12}\t{:^8}\t{:^10}\t{:^10}\t{:^10}"

? ? print(format_title.format("ID","名字","英語成績","Python成績","C語言成績","總成績"))

format_data ="{:^6}{:^12}\t{:^12}\t{:^12}\t{:^12}\t{:^12}"

? ? for infoin studentList:

print(format_data.format(info.get("id"), info.get("name"),str(info.get("english")),str(info.get("python")),

str(info.get("c")),

str(info.get("english") + info.get("python") + info.get("c")).center(12)))

if __name__ =="__main__":

main()

--------------

微信:如火如荼 ? ?歡迎一起探討Python

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末爽航,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌岳掐,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,591評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件饭耳,死亡現(xiàn)場離奇詭異串述,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)寞肖,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,448評論 3 392
  • 文/潘曉璐 我一進(jìn)店門纲酗,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人新蟆,你說我怎么就攤上這事觅赊。” “怎么了琼稻?”我有些...
    開封第一講書人閱讀 162,823評論 0 353
  • 文/不壞的土叔 我叫張陵吮螺,是天一觀的道長。 經(jīng)常有香客問我帕翻,道長鸠补,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,204評論 1 292
  • 正文 為了忘掉前任嘀掸,我火速辦了婚禮紫岩,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘睬塌。我一直安慰自己泉蝌,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,228評論 6 388
  • 文/花漫 我一把揭開白布揩晴。 她就那樣靜靜地躺著勋陪,像睡著了一般。 火紅的嫁衣襯著肌膚如雪文狱。 梳的紋絲不亂的頭發(fā)上粥鞋,一...
    開封第一講書人閱讀 51,190評論 1 299
  • 那天,我揣著相機(jī)與錄音瞄崇,去河邊找鬼呻粹。 笑死,一個(gè)胖子當(dāng)著我的面吹牛苏研,可吹牛的內(nèi)容都是我干的等浊。 我是一名探鬼主播,決...
    沈念sama閱讀 40,078評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼摹蘑,長吁一口氣:“原來是場噩夢啊……” “哼筹燕!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,923評論 0 274
  • 序言:老撾萬榮一對情侶失蹤撒踪,失蹤者是張志新(化名)和其女友劉穎过咬,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體制妄,經(jīng)...
    沈念sama閱讀 45,334評論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡掸绞,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,550評論 2 333
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了耕捞。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片衔掸。...
    茶點(diǎn)故事閱讀 39,727評論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖俺抽,靈堂內(nèi)的尸體忽然破棺而出敞映,到底是詐尸還是另有隱情,我是刑警寧澤磷斧,帶...
    沈念sama閱讀 35,428評論 5 343
  • 正文 年R本政府宣布振愿,位于F島的核電站,受9級特大地震影響瞳抓,放射性物質(zhì)發(fā)生泄漏埃疫。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,022評論 3 326
  • 文/蒙蒙 一孩哑、第九天 我趴在偏房一處隱蔽的房頂上張望栓霜。 院中可真熱鬧,春花似錦横蜒、人聲如沸胳蛮。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,672評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽仅炊。三九已至,卻和暖如春澎蛛,著一層夾襖步出監(jiān)牢的瞬間抚垄,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,826評論 1 269
  • 我被黑心中介騙來泰國打工谋逻, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留呆馁,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,734評論 2 368
  • 正文 我出身青樓毁兆,卻偏偏與公主長得像浙滤,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子气堕,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,619評論 2 354