妳可以試試這個----------------------------------------------------------
# -*- coding:UTF-8 -*-
students=[]
def meun():
print("="*30)
print("*"*10+"學生信息管理"+"*"*10)
print("1.添加學生信息")
print("2.刪除學生信息")
print("3.指定學號查詢學生信息")
print("4.查詢全部學生信息")
print("5.保存信息")
print("0.退出系統")
print("="*30)
def add_new_info():
global students
print("您選擇了添加學生信息功能")
name = input("請輸入學生姓名:")
stuId = input("請輸入學生學號(學號不可重復):")
age = input("請輸入學生年齡:")
#驗證學號是否唯壹
i = 0
leap = 0
for temp in students:
if temp['id'] == stuId:
leap = 1
break
else:
i = i + 1
if leap == 1:
print("輸入學生學號重復,添加失敗!")
break
else:
# 定義壹個字典,存放單個學生信息
stuInfo = {}
stuInfo['name'] = name
stuInfo['id'] = stuId
stuInfo['age'] = age
# 單個學生信息放入列表
students.append(stuInfo)
print("添加成功!")
def del_info():
global students
print("您選擇了刪除學生功能")
delId=input("請輸入要刪除的學生學號:")
#i記錄要刪除的下標,leap為標誌位,如果找到leap=1,否則為0
i = 0
leap = 0
for temp in students:
if temp['id'] == delId:
leap = 1
break
else:
i=i+1
if leap == 0:
print("沒有此學生學號,刪除失敗!")
else:
del students[i]
print("刪除成功!")
def search_info():
global students
searchID=input("請輸入妳要查詢學生的學號:")
#驗證是否有此學號
i = 0
leap = 0
for temp in students:
if temp['id'] == searchID:
leap = 1
break
else:
i = i + 1
if leap == 0:
print("沒有此學生學號,查詢失敗!")
else:
print("找到此學生,信息如下:")
print("學號:%s\n姓名:%s\n年齡:%s\n"%(temp['id'],temp['name'],temp['age']))
def print_all_info():
print("序號\t\t學號\t\t姓名\t\t年齡")
for temp in students:
print("sno:%s,stuName:%s,stuAge:%s"? %(temp['id'],temp['name'],temp['age']))
print("*"*20)
def loda_data():
#加在之前存儲的數據
global students
f = open("info_data.data")
content = f.read()
info_list = eval(content)
f.close()
def main():
#加在數據(先存好數據,在打開這個數據直接讀取數據)
#load_data()
while True:
#1.打印工程
meun()
#2.獲取用戶的選擇
key = input("請輸入要進行的操作):")
#3.根據用戶的選擇,作出相應的事件
if key == "1":
add_new_info()
elif key == "2":
del_info()
elif key == "3":
search_info()
elif key == "4":
print_all_info()
elif key == "5":
save_data()
elif key == "0":
exit_flag = input("妳確定要退出嗎?(yes or no)")
if exit_flag == "yes":
break
else:
print("輸入有誤,請重新輸入。。。")
input("\n\n\n按回車鍵可以繼續。。。")
continue
#程序開始
main()
摘自?網頁鏈接-------------------------------------------------------------------
請采納,謝謝