功能模塊分析
1.首頁(菜單功能)
2.添加學生信息
3.刪除學生信息
4.顯示學生信息
5.修改學生信息
6.按照學生年齡排序
代碼如下:
def main(): while True: printmenu() #打印菜單 number = int(input("請輸入功能對應的數字:")) if number == 1: addInfo() #添加學生信息 elif number == 2: delInfo() #刪除學生信息 elif number == 3: showInfo() #顯示學生信息 elif number == 4: modInfo() #修改學生信息 elif number == 5: rise_hum() #按照學生學號由低-高排序 elif number == 6: down_hum() #按照學生學號由高-低排序 elif number == 0: qut = input("輸入yes退出系統,輸入其他任意字符不退出:") if qut == "yes": break stuInfo=[] #定義一個空列表,用於存放所有學生信息 def printmenu(): menu=""" ====================學生信息管理系統==================== 1.添加學生信息 2.刪除學生信息 3.顯示學生信息 4.修改學生信息 5.按照學生學號由低-高排序 6.按照學生學號由高-低排序 0.退出系統 ===================================================== """ print(menu) def addInfo(): while True: newname = input("請輸入需添加學生的姓名:") newsex = input("請輸入需添加學生的性別:") newage = input("請輸入需添加學生的年齡;") newhum = input("請輸入需添加學生的學號:") newInfo={} #定義一個空字典,用於存放一個學生的信息 newInfo["name"] = newname newInfo["sex"] = newsex newInfo["age"] = newage newInfo["hum"] = newhum stuInfo.append(newInfo) #將含有學生信息的字典添加到列表中 qut = input("輸入no結束添加學生信息,輸入其他任意字符將繼續:") if qut == "no": break def delInfo(): while True: delnumber=int(input("請輸入需刪除的學生序號:"))-1 del stuInfo[delnumber]["name"] #刪除學生的姓名 del stuInfo[delnumber]["sex"] #刪除學生的性別 del stuInfo[delnumber]["age"] #刪除學生的年齡 del stuInfo[delnumber]["hum"] #刪除學生的學號 qut = input("輸入no結束刪除學生信息,輸入其他任意字符將繼續:") if qut == "no": break def showInfo(): for item in stuInfo: print('---------------------------') print(item["name"],item["sex"],item["age"],item["hum"]) print('---------------------------') def modInfo(): while True: modnumber = int(input("請輸入需修改學生的序號:"))-1 modname = input("請輸入修改后學生的姓名:") modsex = input("請輸入修改后學生的性別:") modage = input("請輸入修改后學生的年齡:") modhum = input("請輸入修改后學生的學號:") stuInfo[modnumber]["name"] = modname #修改學生的成績 stuInfo[modnumber]["sex"] = modsex #修改學生的性別 stuInfo[modnumber]["age"] = modage #修改學生的年齡 stuInfo[modnumber]["hum"] = modhum #修改學生的學號 qut = input("輸入no結束修改學生信息,輸入其他任意字符將繼續:") if qut == "no": break def rise_hum(): print("按照學生學號由低-高顯示:") result_1 = sorted(stuInfo,key=lambda x:x["hum"]) for item in result_1: print('---------------------------') print(item["name"],item["sex"],item["age"],item["hum"]) print('---------------------------') def down_hum(): print("按照學生學號由高-低顯示") result_2 = sorted(stuInfo, key=lambda y:y["hum"],reverse=True) for item in result_2: print('---------------------------') print(item["name"],item["sex"],item["age"],item["hum"]) print('---------------------------') main()