1. 程式人生 > >python 學習筆記-學生資訊線上管理

python 學習筆記-學生資訊線上管理

#學員資訊線上管理系統

#定義一個用於存放學員資訊的  列表  變數
stulist=[
	{"name":"zhangsan","age":20,"classnum":"py1"},
	{"name":"lisi","age":21,"classnum":"py2"},
	{"name":"wangwu","age":22,"classnum":"py3"}]

#定義一個學生資訊的輸出函式
def showstu(stulist):
	'''
	學生資訊的輸出函式
	'''
	
	if len (stulist)==0:
		print ("==================無學員資訊!======================")
		return
	print ("|{0:<5} | {1:<10} | {2:<5} | {3:<10}|".format("sid","name","age","classnum"))
	print ("-"*50)
	for i in range(len(stulist)):
		print ("|{0:<5} | {1:<10} | {2:<5} | {3:<10}|".format(i+1,stulist[i]["name"],stulist[i]["age"],stulist[i]["classnum"]))



while True:
	print ("="*14,"學員資訊線上管理系統","="*14)
	print ("{0:1}{1:18}{2:18}".format(" ","1.檢視學員資訊","2.新增學員資訊"))
	print ("{0:1}{1:18}{2:18}".format(" ","3.刪除學員資訊","4.退出操作"))
	print ("="*50)

	key = input("請輸入對應的選擇:")

	if key == "1":
		print ("="*14,"學員資訊檢視","="*22)
		showstu(stulist)
		input("按回車鍵繼續")
	elif key == "2":
		print ("="*14,"學員資訊新增","="*22)
		stu={}
		stu["name"]=input("請輸入要新增的學員姓名:")
		stu["age"]=input("請輸入要新增的學員年齡:")
		stu["classnum"]=input("請輸入要新增的學員班級:")
		stulist.append(stu)
		showstu(stulist)
		input("按回車鍵繼續")
	elif key == "3":
		print ("="*14,"學員資訊刪除","="*22)
		sid = input("請輸入要刪除的學員資訊id號")
		del stulist[int(sid)-1]
		showstu(stulist)
		input("按回車鍵繼續")
	elif key == "4":
		print ("==================== 再見! ======================")
		break