Python function to realize student management system

Time:2021-10-22

This example shares the specific code of Python function to realize the student management system for your reference. The specific contents are as follows

This is a simple management program

Enter name, age, gender (you can also add other categories, such as sexual orientation), and then save it as a list (empty list by default). The functions are as follows:

  • Press 1 to add members
  • Press 2 to delete the member
  • Press 3 to modify the member information (name is not included at present, but can be added)
  • Press 4 to retrieve and print all information about a member
  • Press 5 to print the information of all members
  • Press 6 to exit the program

That’s about it. Here is the code:

import time

def main():
  '' main function '' '
  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():
  '' display system functions' ''
  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():
  '' add new person '' '
  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():
  '' delete existing people '' '
  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():
  '' modify existing person information '' '
  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():
  '' find information about a person '' '
  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():
  '' unified name initials are capitalized and left justified '' '
  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():
  '' output all information of all characters' ''
  modify_op()
  for i in all_info:
    print('\n',i,'\n')

def sl():
    time.sleep(0.5)

all_info = []
main()

Briefly explain:

  • Since there is no switch case statement in Python, if elif is used instead to realize the requirement of key selection function
  • The storage of information is realized by nesting lists and dictionaries, that is, in the format of [{}, {}, {}]. Each dictionary stores the information of a person.
  • Function modify_ Op () is to beautify the display and realize the output of the names of all members with equal length, capital letters and left alignment. For example, norn and Scotti, the latter has 6 characters and the former has 4 characters. In this way, norn will be supplemented by 6 characters with spaces. This is why strip () and title () are used in a large number of functions
  • Some codes are used to avoid bugs, such as excluding the influence of case (vert and vert), excluding the possibility of duplicate names, excluding errors caused by inputting characters other than 1-6 when selecting program functions, and excluding various dead cycles.
  • Since the program does not have the function of saving information to a text file, all information will disappear after exiting the program. Because I use Google’s cloud IDE, I don’t know how to write the path to save the file (/ gddrive / file. TXT?). So when your computer can be used, it will be added

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support developpaer.