Python函數(shù)實現(xiàn)學員管理系統(tǒng)
發(fā)布日期:2022-02-22 15:25 | 文章來源:源碼之家
本文實例為大家分享了Python函數(shù)實現(xiàn)學員管理系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下
這個是一個簡單的管理程序
輸入姓名,年齡,性別(也可以添加其他類別例如性取向),然后以列表的形式保存(默認為空列表)。功能如下:

- 按1添加成員
- 按2刪除成員
- 按3修改成員信息(目前不包括姓名但是可以添加)
- 按4檢索并打印某個成員的全部信息
- 按5打印全部成員的信息
- 按6退出程序
大概就是這樣子。下面是代碼:
import time
def main():
'''主函數(shù)'''
while True:
sl(),select_function()
sl()
user_input = input('select your operation: ')
ifuser_input == '1':
sl(),add_op()
elif user_input == '2':
sl(),delete_op()
elif user_input == '3':
sl(),alter_op()
elif user_input == '4':
sl(),search_op()
elif user_input == '5':
sl(),print_op()
elif user_input == '6':
print('\n system quit.')
break
else:
sl(),print('\n plz enter correct number.')
def select_function():
'''顯示系統(tǒng)功能'''
print("\n1.add mbr\n2.delete mbr\n3.change info\
\n4.check info\n5.prt\'l info\n6.exit sys\n")
sl()
def store_new_info():
a = input('enter name: ').title()
b = input('enter age: ').title()
c = input('enter gender: ').title()
return a,b,c
def add_op():
'''添加新人'''
name,age,gender = store_new_info()
for i in all_info:
if name == i['name'].strip():
print(f'{name} is existed.retry plz')
break
else:
dict_inf = {}
dict_inf['name']= name
dict_inf['age'] = age
dict_inf['gender'] = gender
all_info.append(dict_inf)
print(f'{name} added.')
def delete_op():
'''刪除已有人物'''
del_nam = input('type the name to del:').title()
for i in all_info:
if del_nam == i['name'].strip():
all_info.remove(i)
sl(),print(f'{del_nam} is removed.')
else:
sl(),print(f'no {del_nam} in list now.')
def alter_op():
'''修改現(xiàn)有人物信息'''
alter_nam = input('type the name who needs change: ').title()
for i in all_info:
if alter_nam != i['name'].strip():
continue
else:
i['age'] = input('type new age: ')
i['gender'] = input('type new gender: ')
break
else:
sl(),print(f'no {alter_nam} in list.')
def search_op():
'''查找某個人物的信息'''
se_num = input('type name to search: ').strip().title()
for i in all_info:
if se_num != i['name'].strip():
continue
else:
sl(),print(i)
break
def modify_op():
'''統(tǒng)一name首字母大寫且左對齊'''
b = 0
for i in range(len(all_info)):
a = len(all_info[i].get('name').strip())
b = max(a,b)
for i in range(len(all_info)):
all_info[i]['name']= all_info[i].get('name').strip().title().ljust(b,' ')
all_info[i]['gender'] = all_info[i].get('gender').strip().title().ljust(6,' ')
def print_op():
'''輸出所有人物的全部信息'''
modify_op()
for i in all_info:
print('\n',i,'\n')
def sl():
time.sleep(0.5)
all_info = []
main()
簡單解釋一下:
- 由于python中沒有switch case語句所以這里用if elif代替,實現(xiàn)按鍵選擇功能的需求
- 信息的保存是用列表和字典嵌套實現(xiàn),即形如 [{},{},{}] 的格式,每個字典里面保存一個人員的信息。
- 函數(shù)modify_op()是為了美化顯示,實現(xiàn)把所有成員的名字取等長,首字母大寫且左對齊輸出。例如norn和scotti,后者6個字符前者4個字符,這樣就會用空格把norn補為6個字符。函數(shù)中大量出現(xiàn)的strip()和title()就是為此才使用的
- 用了一些代碼來避免bug,例如排除大小寫的影響(vert和VERT),排除重名的可能性,排除選擇程序功能時輸入1-6之外的字符導致報錯,排除各種的死循環(huán)等。
- 由于程序沒有保存信息到文本文件的功能,所以退出程序之后所有的信息都會消失。因為我是用的是谷歌的云ide,不知道保存文件路徑怎么寫(/gdrive/file.txt?)。so啥時候自己的電腦能拿來用了就會加上的
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持本站。
版權聲明:本站文章來源標注為YINGSOO的內(nèi)容版權均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務器上建立鏡像,否則將依法追究法律責任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學習參考,不代表本站立場,如有內(nèi)容涉嫌侵權,請聯(lián)系alex-e#qq.com處理。
相關文章
關注官方微信