import time import them # Set a list to store all student information (each student is a dictionary) info_list = [] def print_menu(): print("---------------------------") print("Student Management System V1.0") print("1: add student") print("2: delete student") print("3: Modify students") print("4: query students") print("5: show all students") print("6: save data") print("7: Exit the system") print("---------------------------") def add_new_info(): """Add student information""" global info_list new_name = input("Please enter your name:") new_tel = input("Please enter your phone number:") new_qq = input("Please enter QQ:") for temp_info in info_list: if temp_info['name'] == new_name: print("This username is already occupied, please re-enter") return # If a function only has a return, it is equivalent to letting the function end without a return value # Define a dictionary to store the user's student information (this is a dictionary) info = {} # add data to dictionary info["name"] = new_name info["tel"] = new_tel info["qq"] = new_qq # add this dictionary to the list info_list.append(info) def del_info(): """Delete student information""" global info_list del_num = int(input("Please enter the serial number to delete:")) if 0 <= del_num < len(info_list): del_flag = input("Are you sure you want to delete? yes or no") if del_flag == "yes": from info_list [from_num] else: print("The input serial number is wrong, please re-enter") def modify_info(): """Modify student information""" global info_list modify_num = int(input("Please enter the serial number to be modified:")) if 0 <= modify_num < len(info_list): print("The information you want to modify is: ") print("name:%s, tel:%s, QQ:%s" % (info_list[modify_num]['name'], info_list[modify_num]['tel'],info_list[modify_num]['qq'])) info_list[modify_num]['name'] = input("Please enter a new name:") info_list[modify_num]['tel'] = input("Please enter a new phone number:") info_list[modify_num]['qq'] = input("Please enter a new QQ:") else: print("The input serial number is wrong, please re-enter") def search_info(): """Query student information""" search_name = input("Please enter the name of the student you want to query:") for temp_info in info_list: if temp_info['name'] == search_name: print("The query information is as follows:") print("name:%s, tel:%s, QQ:%s" % (temp_info['name'], temp_info['tel'], temp_info['qq'])) break else: print("There is no information you are looking for....") def print_all_info(): """Traverse student information""" print("Serial number\tName\t\tMobile number\t\tQQ") i = 0 for temp in info_list: # temp is a dictionary print("%d\t%s\t\t%s\t\t%s" % (i, temp['name'], temp['tel'], temp['qq'])) i += 1 def save_data(): """Load previously stored data""" f = open("info_data.data", "w") f.write(str(info_list)) f.close() def load_data(): """Load previously stored data""" global info_list f = open("info_data.data") content = f.read() info_list = eval(content) f.close() def main(): """Used to control the entire process""" # Load data (only once) load_data() while True: # 1. Print function print_menu() # 2. Get the user's choice num = input("Please enter the operation to be performed (number):") # 3. According to the user's choice, do the corresponding things if num == "1": # add students add_new_info() elif num == "2": # delete student del_info () elif num == "3": # modify students modify_info() elif num == "4": # query students search_info() elif num == "5": # loop through all the information print_all_info() elif num == "6": # save data to file save_data() elif num == "7": # Exit system exit_flag = input("Dear, are you sure you want to exit?~~~~(>_<)~~~~(yes or no) ") if exit_flag == "yes": break else: print("Incorrect input, please re-enter...") os.system('cls') # start of program main()