The use of Python loop

Keywords: Python

Recently, I was learning Python, trying to make a simple student management system with for and while loops. Now I have encountered the problem that I can't define the input data type and jump out of the for loop

The first restriction is: if there is no integer input, the program directly reports an error, in which the conversion is used, code 18 lines

The second jump out of for loop: the purpose is to make a preliminary judgment on the entered information to ensure that the key information is not repeated

This management system involves defining the dictionary grade first, adding the dictionary grade to the list student using the append() function, and then traversing the array through the for loop

The dictionary takes out the sname, and finally compares the entered name with the value corresponding to the key in the dictionary sname, so as to achieve the data matching query

  1 # print absolute value of an integer
  2 '''If you change the system to query results, how to do it, and how to stack dictionaries'''
  3 
  4             #Definition information list
  5 
  6 print('Welcome to the student achievement management system!\n\t************\n Please check what you can do\n\t************')
  7 menu = ('1.Input','2.query','3.modify','4.delete','5.preview','6.Sign out')
  8 orderid = (1,2,3,4,5,6)    #Step selection of operation
  9 student = []
 10 a = 1
 11 m = 0
 12 n = 0
 13 while a == 1:                        #The purpose is to keep the operation in use, cycle and exit without operation
 14     for opration in menu:# Limit input integers
 15         print(opration)
 16     order = input('Please input the serial number to do the operation you want:')
 17     '''try:
 18         order = int(order)
 19     except TypeError as e:
 20         print('Error reason:%s' % e)
 21         continue'''
 22 
 23 
 24     if int(order) in orderid:
 25 
 26         while int(order) == 1:
 27             name = input('Please enter the student's name:')# Data entry#Need to solve the problem of repeated entry
 28             '''for stu_name in student:
 29                 if name == stu_name['Full name']:
 30                     stu_id = input('The student has entered information, please confirm whether to re-enter,And compare student numbers')
 31                     if stu_id == stu_name['Student ID']:
 32                         print('Duplicate entry! Please re-enter student information')
 33                 continue'''
 34 
 35             id = input('Please enter the student number:')
 36             classid = input('Please enter the class:')
 37             Chinese = float(input('Please enter your Chinese score:'))
 38             Math = float(input('Please enter your math score:'))
 39             English = float(input('Please enter your English score:'))
 40             Physical = float(input('Please enter the physical score:'))
 41             Chemistry = float(input('Please enter chemistry score:'))
 42             Biological = float(input('Please enter your biology score:'))
 43             Score = round((Chinese * 3 + Math * 3 + English * 3 + Physical * 2 + Chemistry * 2 + Biological * 2) / 15,2)
 44 
 45             #Define a new dictionary
 46             grade  = {}
 47             grade['Full name'] = name
 48             grade['Student ID'] = id
 49             grade['class'] = classid
 50             grade['Chinese'] = Chinese
 51             grade['Mathematics'] = Math
 52             grade['English?'] = English
 53             grade['Physics'] = Physical
 54             grade['Chemistry'] = Chemistry
 55             grade['Biology'] = Biological
 56             grade['Weighted total score'] = Score
 57 
 58             # Add a dictionary to the list
 59             student.append(grade)
 60 
 61             print('You have successfully entered a message. Please press Y,To return to the menu, press N')
 62             exit = input()
 63             if exit == 'y':
 64                 continue
 65             else:
 66                 break
 67 
 68 
 69         while int(order) == 2:
 70 
 71             name = input("Please enter the name of the student you want to find:")
 72             n += 1
 73             if n > 2:
 74                 print('You have repeatedly entered the wrong name three times. Please confirm the name and query again')
 75                 n = 0
 76                 continue
 77 
 78             t_name = 0#Enter the correct flag bit
 79 
 80             for sname in student:
 81                 if name == sname['Full name']:
 82                     print('The student's record is:',sname)
 83                     #print('%s Student's record: student number%s class%s Chinese%.2f Mathematics%.2f English?%.2f Physics%.2f Chemistry%.2f Biology%.2f '
 84                           #'Weighted total score%.2f' % (name,sname['Student ID'],sname['class'],sname['Chinese'],sname['Mathematics'],sname['Physics'],sname['Chemistry'],sname['Biology'],sname['Weighted total score']))
 85 
 86                     t_name = 1
 87 
 88                     break
 89 
 90             if t_name == 0:
 91                 exit = input('Query error, please confirm the name of the student you want to query again. To return to the main menu, please press N')
 92                 if exit == 'n':
 93                     break
 94                 continue
 95             else:
 96                 exit = input('The query is successful. Enter the name to continue the query. To return to the main menu, press N')
 97                 if exit == 'n':
 98                     break
 99                 continue
100 
101 
102         while int(order) == 3:
103             name = input("Please enter the name of the student you want to modify:")
104 
105             t_name = 0  # Enter the correct flag bit
106 
107             for sname in student:
108                 if name == sname['Full name']:
109                     id = input('Please enter the student number:')
110                     classid = input('Please enter the class:')
111                     Chinese = float(input('Please enter your Chinese score:'))
112                     Math = float(input('Please enter your math score:'))
113                     English = float(input('Please enter your English score:'))
114                     Physical = float(input('Please enter the physical score:'))
115                     Chemistry = float(input('Please enter chemistry score:'))
116                     Biological = float(input('Please enter your biology score:'))
117                     Score = round(
118                         (Chinese * 3 + Math * 3 + English * 3 + Physical * 2 + Chemistry * 2 + Biological * 2) / 15, 2)
119 
120                     grade['Full name'] = name
121                     grade['Student ID'] = id
122                     grade['class'] = classid
123                     grade['Chinese'] = Chinese
124                     grade['Mathematics'] = Math
125                     grade['English?'] = English
126                     grade['Physics'] = Physical
127                     grade['Chemistry'] = Chemistry
128                     grade['Biology'] = Biological
129                     grade['Weighted total score'] = Score
130 
131                     t_name = 1
132                     break
133 
134             if t_name == 0:
135                 exit = input('Query error, please reconfirm the name of the student to be modified. To return to the main menu, please press N')
136                 if exit == 'n':
137                     break
138                 continue
139             else:
140                 exit = input('The query is successful. Enter the name to continue the query. To return to the main menu, press N')
141                 if exit == 'n':
142                     break
143                 continue
144 
145 
146         while int(order) == 4:
147 
148             name = input("Please enter the name of the student you want to delete:")
149 
150             t_name = 0  # Enter the correct flag bit
151 
152             for sname in student:
153                 if name == sname['Full name']:
154                     student.remove(sname)
155                     t_name = 1
156                     break
157             if t_name == 0:
158                 exit = input('Query error, please reconfirm the name of the student to be deleted. To return to the main menu, please press N')
159                 if exit == 'n':
160                     break
161                 continue
162             else:
163                 exit = input('Query succeeded, input name to continue to delete, return to the main menu, please press N')
164                 if exit == 'n':
165                     break
166                 continue
167 
168         while int(order) == 5:
169             if len(student) == 0:
170                 print('There is no data in the system. Please record the data')
171                 break
172             else:
173                 for s_name in student:
174                     print(s_name)
175             exit = input('Preview succeeded! Press any key to return to the main menu\n')
176             break
177 
178         if int(order) == 6:
179             print('Thank you for your use. Goodbye!\n')
180             break
181 
182 
183     else:
184         print('Input error, please input No. 1~6')
185         m += 1
186         if m > 2:
187             print('You have repeated the wrong input three times to end this operation')
188             break
189         continue

Posted by evanesq on Wed, 01 Apr 2020 01:19:16 -0700