Just learned python, practice code, and python practice code
1 # coding: UTF-8 2 import OS 3 # declare an empty list to store the student's name 4 member_list = [] 5 # Read the local file data, adding local data to member_list does not guarantee the second running of member_list 6 if OS .path.exists('student.txt '): 7 file_test = open('student.txt', 'R') 8 name_list = file_test.readlines () 9 # process the name in name_list and remove \ n 10 for name in name_list: 11 res = name. strip ('\ n') 12 # Add the processed name to member-list 13 member_list.append (res) 14 file_test.close () 15 el Se: 16 print 'file does not exist! '17 while 1: 18 print "" 19 1. add Student name 20 2. modify Student name 21. 3. query the Student name 22 4. delete Student name 23 0. exit program 24 "25 select_operation = input ('enter operation number') 26 # cyclically determine whether the input number exists 27 while select_operation <0 or select_operation> 4: 28 select_operation = input ('Operation No. exists, please enter operation no. ') 29 # determine each serial number and set the corresponding logic 30 if select_operation = 1: 31 # add operation 32 member_name = raw_input ('enter Student name: ') 33 # Add name to list 34 member_list.append (member_name) 35 prin T' ----- added \ n' 36 elif select_operation = 2: 37 # modify the Student name 38 # first query all the student names, and then set the number for each student, 39 for x in xrange (0, len (member_list): 40 # first, use x as the index to remove each value in the list, 41 name = member_list [x] 42 print x + 1 ,'. ', name 43 # obtain the entered student ID, because you need to modify the Student name information according to the ID. 44 select_number = input ('Enter the student ID to modify: ') 45 # Check whether the cyclic check number is correct 46 while select_number <1 or select_number> len (member_list ): 47 select_number = input ('student ID does not exist, re-enter: ') 48 # Modify the data in the list according to the retrieved number 49 # obtain the new student name entered on the console 50 new_name = raw_input ('Enter the modified name :') 51 member_list [select_number-1] = new_name 52 print 'data modified successfully! N '53 elif select_operation = 3: 54 print '1-Query Information '55 print' 2-query all students '56 select_number = input ('select the query operation: ') 57 while select_number! = 1 and select_number! = 2: 58 select_number = input ('select query operation again: ') 59 if select_number = 1: 60 number = input ('enter query ID :') 61 while number <1 or number> len (member_list): 62 number = input ('student ID does not exist, re-enter: ') 63 print member_list [number-1] 64 else: 65 for x in xrange (0, len (member_list): 66 # first, use x as the index to remove each value in the list. 67 name = member_list [x] 68 print x + 1, '. ', name 69 elif select_operation = 4: 70 print '1-delete '71 print' 2-delete by name' 72 print '3-delete all students '73 number = input ('select delete operation: ') 74 while number! = 1 and number! = 2 and number! = 3: 75 number = input ('select the correct delete operation: ') 76 if number = 1: 77 num = input ('Enter the student serial number :') 78 while num <0 or num> len (member_list): 79 num = input ('Enter the correct number of incoming students: ') 80 member_list.pop (num) 81 print 'deleted successfully \ n' 82 elif number = 2: 83 name = raw_input ('enter name: ') 84 # Check whether the name is in the list, if the flase condition is false, 85 while name not in member_list: 86 name = raw_input ('name does not exist, re-enter: ') 87 member_list.remove (name) is returned) 88 pri Nt 'deleted successfully \ n' 89 else: 90 while len (member_list): 91 del member_list [0] 92 print 'deleted successfully \ n' 93 else: 94 # exit 95 break 96 # because the member_list list stores the latest data after addition, deletion, modification, and query, and directly writes all the data in the list to a local file. 97 file_test = open('student.txt ', 'w') 98 for x in xrange (0, len (member_list): 99 name = member_list [x] 100 # after obtaining the name, each cycle writes the name to the local device. 101 file_test.write (name) 102 file_test.write ('\ n') 103 # close the file 104 file_test.close ()