This system was created by myself , Hope to help beginners learn Python Little buddy , Code changes the world , Technology realizes freedom !
Function block
(1). Add student information
(2). Modify student information
(3). Search for student information
(4). Delete student information
(5). View all student information
(6). Save to file
(7). sign out
Realize the idea :
1. Define a student class , Contains multiple functions ( menu 、 increase 、 Delete 、 Change 、 check 、 The main function ), Enhance the modularity of the program 、 Readability .
2. Define a static member variable in the function ( An empty dictionary ), Used to store student information . The function of the dictionary is to avoid entering duplicate student numbers
3. File function usage txt The relative position of the file is saved , If txt file does not exist , The program is created automatically . If the file exists, empty the contents of the file ( If you don't want to empty , You can change the way you open files , This system uses w+ The way )
Class function code :
Add student information
def add(self):
sid = input(' Please enter the student number :')
if sid in self.map:
print(" The student number already exists , Please check !")
return
name = input(' Please enter a name :')
sex = input(' Please enter gender :')
tel = input(' Please input the phone number :')
address = input(' Please enter your home address :')
list = [sid, name, sex, tel, address] # Load data to list list
self.map.update({sid:list}) # Update dictionary collection
print(" Add success !")
print(self.map)
Modify student information
def alter(self):
key = input(' Please enter the student number :')
if key not in self.map:
print(" The student number doesn't exist , Please check ")
return
self.map.get(key)[1] = input(' Please enter a name :')
self.map.get(key)[2] = input(' Please enter gender :')
self.map.get(key)[3] = input(' Please input the phone number :')
self.map.get(key)[4] = input(' Please enter your home address :')
print(" Modification successful !")
Search for student information
def seek(self):
key = input(' Please enter the student number :')
if key in self.map:
for elm in self.map.get(key):
sys.stdout.write(elm + " ")
print("")
else:
print(" The student number doesn't exist ! Please check ")
Delete student information
def delete(self):
sid = input(' Please enter the student ID to delete the corresponding information :')
if sid in self.map:
dict = self.map.pop(sid)
print(" Delete successful !")
else:
print(" The student number doesn't exist , Please check ")
View all student information
def showAll(self):
print(" Student number full name Gender Telephone The earth site ")
for key in self.map: # Traverse key value
for elm in self.map.get(key): # adopt key Value traversal value value , value Just a student list list
sys.stdout.write(elm + " ")
print("")
Save to file
def saveToFile(self):
file = open(fileName,"w+") # w+ Indicates if the file does not exist , Then create a . Empty the contents of the file if it exists
for key in self.map:
for elm in self.map.get(key):
file.write(elm + " ")
file.write("\n")
file.close() # Close file
print(" Saved successfully !")
Running effect :
Focus on ! Complete code , The official account of WeChat 《 Source Inn 》 reply pystu Get the source code link !, If you have any questions about the source code, you can confide in me , See me will reply positively ! Like friends can like + Pay attention to !