您好,登錄后才能下訂單哦!
import os
stus = [] # 學生信息列表(每個學生的信息以字段存儲)
def add_stu():
sid = input('輸入學生學號:')
sname = input('輸入學生姓名:')
sage = input('輸入學生年齡:')
dict_stu = {'id': sid.strip(), 'name': sname.strip(), 'age': int(sage.strip())}
stus.append(dict_stu)
def remove_stu(name):
item = search_stu(name)
stus.remove(item)
def search_stu(name):
flag = False
for s in stus:
if s['name'] == name.strip():
print(s)
flag = True
return s
if flag == False:
print('該學生不存在')
return None
def query_all_stu():
print('序號\tid\tname\tage')
for i, stu in enumerate(stus, 1):
print(i, end="")
print('\t%s\t%s\t%s' % (stu['id'], stu['name'], stu['age']))
def print_menu():
print('=' * 30)
print('學生管理系統'.center(30))
print('輸入1:添加學生')
print('輸入2:查找學生')
print('輸入3:修改學生')
print('輸入4:刪除學生')
print('輸入5:查看所有學生')
print('輸入6:退出')
def file_read():
try:
file_name=r'stu.txt'
if os.path.exists(file_name):
f = open(file_name, 'r', encoding='utf-8')
while True:
line_text = f.readline()
if line_text == "":
break
line_list = line_text.split()
dict_item={'id':line_list[0],'name':line_list[1],'age':line_list[2]}
stus.append(dict_item)
print(line_list)
f.close()
else:
print('file is not exist.')
except Exception as e:
print(e)
finally:
print('file_read methon is exec completed.')
def file_write():
try:
file_name = r'stu.txt'
if os.path.exists(file_name):
os.remove(file_name)
f = open(file_name, 'w', encoding='utf-8')
for item in stus:
line_text = "%s\t%s\t%s" % (item['id'], item['name'], item['age'])
f.write(line_text)
f.write('\n')
f.close()
except Exception as e:
print(e)
finally:
print('file_write methon exec over..')
def main():
file_read()
while True:
print_menu()
operate = input('請輸入你想要的操作:')
if operate == "1":
add_stu()
elif operate == "2":
sname = input("輸入學生姓名:")
search_stu(sname)
elif operate == "3":
pass
elif operate == "4":
sname = input("請輸入刪除學生的姓名:")
remove_stu(sname)
elif operate == "5":
query_all_stu()
else:
file_write()
print('exit......')
break
main()
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。