# 編寫一個“學生信息管理系統”
# 輸入序號:1. 輸入學生信息,學生信息包括:id,name,age,gender(用什么數據類型保存?)
# 2. 查詢:輸入學生姓名和id,顯示學生個人信息
# 3. 修改:輸入學生姓名或者id,可以對學生信息進行修改
# 4. 刪除:輸入學生姓名或者id,刪除對應學生信息
代碼如下:::
def increase(): student = [input("學號:"), input("姓名:"), input("年齡:"), input("性別:")] students.append(student) def query(): s = input("輸入學生學號或姓名:") if s.isdigit() == True: id = s # enumerate()是python的內置函數、適用於python2.x和python3.x # enumerate在字典上是枚舉、列舉的意思 # enumerate參數為可遍歷/可迭代的對象(如列表、字符串) # enumerate多用於在for循環中得到計數,利用它可以同時獲得索引和值,即需要index和value值的時候可以使用enumerate for station, item in enumerate(students): if id in item: print("此學生信息為:", item) break if station + 1 == len(students): print("查無此人") else: name = s for station, item in enumerate(students): if name in item: print("此學生信息為:", item) break if station + 1 == len(students): print("查無此人") def modify(): s = input("輸入學生學號或姓名:") if s.isdigit() == True: id = s for item in students: if id in item: print(item) while 1: print("--選擇修改具體信息--") n = int(input("請輸入要進行的操作序號:1.學號 2.姓名 3.年齡 4.性別")) if n == 1: item[0] = input("輸入修改后的學號:") elif n == 2: item[1] = input("輸入修改后的姓名:") elif n == 3: item[2] = input("輸入修改后的年齡:") else: item[3] = input("輸入修改后的性別:") print("--修改完成--") break else: name = s for item in students: if name in item: print(item) while 1: print("--選擇修改具體信息--") n = int(input("請輸入要進行的操作序號:1.學號 2.姓名 3.年齡 4.性別")) if n == 1: item[0] = input("輸入修改后的學號:") elif n == 2: item[1] = input("輸入修改后的姓名:") elif n == 3: item[2] = input("輸入修改后的年齡:") else: item[3] = input("輸入修改后的性別:") print("--修改完成--") break def remove(): s = input("輸入學生學號或姓名:") if s.isdigit() == True: id = s for item in students: if id in item: i = students.index(item) del(students[i]) else: name = s for item in students: if name in item: i = students.index(item) del(students[i]) if __name__ == '__main__': students = [["1", "王二麻子", "15", "女"], ["2", "張三", "16", "男"]] while 1: print("-----------------------進入主界面--------------------------") print("---------------1.增加 2.查詢 3.修改 4.刪除-----------------") n = int(input("請輸入:")) if n == 1: increase() #調用增加函數 elif n == 2: query() #調用查詢函數 elif n == 3: modify() #調用修改函數 else: remove() #調用刪除函數 print(students) print("-----------------------------------------------------------")
運行結果如下:
-----------------------進入主界面-------------------------- ---------------1.增加 2.查詢 3.修改 4.刪除----------------- 請輸入:1 學號:3 姓名:李四 年齡:17 性別:男 [['1', '王二麻子', '15', '女'], ['2', '張三', '16', '男'], ['3', '李四', '17', '男']] ----------------------------------------------------------- -----------------------進入主界面-------------------------- ---------------1.增加 2.查詢 3.修改 4.刪除----------------- 請輸入:2 輸入學生學號或姓名:2 此學生信息為: ['2', '張三', '16', '男'] [['1', '王二麻子', '15', '女'], ['2', '張三', '16', '男'], ['3', '李四', '17', '男']] ----------------------------------------------------------- -----------------------進入主界面-------------------------- ---------------1.增加 2.查詢 3.修改 4.刪除----------------- 請輸入:3 輸入學生學號或姓名:3 ['3', '李四', '17', '男'] --選擇修改具體信息-- 請輸入要進行的操作序號:1.學號 2.姓名 3.年齡 4.性別2 輸入修改后的姓名:周五 --修改完成-- [['1', '王二麻子', '15', '女'], ['2', '張三', '16', '男'], ['3', '周五', '17', '男']] ----------------------------------------------------------- -----------------------進入主界面-------------------------- ---------------1.增加 2.查詢 3.修改 4.刪除----------------- 請輸入:4 輸入學生學號或姓名:3 [['1', '王二麻子', '15', '女'], ['2', '張三', '16', '男']] -----------------------------------------------------------
還有許多可以優化的地方,等我繼續學習一下哈哈