Python函数实现学员管理系统
作者:bluenessdrops
这篇文章主要为大家详细介绍了Python函数实现学员管理系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了Python函数实现学员管理系统的具体代码,供大家参考,具体内容如下
这个是一个简单的管理程序
输入姓名,年龄,性别(也可以添加其他类别例如性取向),然后以列表的形式保存(默认为空列表)。功能如下:

- 按1添加成员
 - 按2删除成员
 - 按3修改成员信息(目前不包括姓名但是可以添加)
 - 按4检索并打印某个成员的全部信息
 - 按5打印全部成员的信息
 - 按6退出程序
 
大概就是这样子。下面是代码:
import time
def main():
  '''主函數'''
  while True:
    sl(),select_function()
    sl()
    user_input = input('select your operation: ')
    if   user_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():
  '''顯示系統功能'''
  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():
  '''修改現有人物信息'''
  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():
  '''統一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代替,实现按键选择功能的需求
 - 信息的保存是用列表和字典嵌套实现,即形如 [{},{},{}] 的格式,每个字典里面保存一个人员的信息。
 - 函数modify_op()是为了美化显示,实现把所有成员的名字取等长,首字母大写且左对齐输出。例如norn和scotti,后者6个字符前者4个字符,这样就会用空格把norn补为6个字符。函数中大量出现的strip()和title()就是为此才使用的
 - 用了一些代码来避免bug,例如排除大小写的影响(vert和VERT),排除重名的可能性,排除选择程序功能时输入1-6之外的字符导致报错,排除各种的死循环等。
 - 由于程序没有保存信息到文本文件的功能,所以退出程序之后所有的信息都会消失。因为我是用的是谷歌的云ide,不知道保存文件路径怎么写(/gdrive/file.txt?)。so啥时候自己的电脑能拿来用了就会加上的
 
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
