程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python implementation of student information management (object-oriented)

編輯:Python

Catalog

The official code is as follows :

student.py—— Be responsible for keeping a single piece of student information

student_manage.py—— Responsible for managing student information , Operate the student information

main.py—— Program entry file


Realize the increase of student information 、 Delete 、 Change 、 check 、 Read in from a file 、 write file

Found out python An easy for programmers to write bug The place of :

If file=open('students.txt', 'r')

When using file.readlines() Method when reading data from a file , No matter how the program is written , Just use it once file.readlines(), Then I used file.readlines() The returned results will become an empty list !

As shown in the figure , Four lines of data have been saved in the text

Before I use the following code to output file.readlines() After the length of the returned list , Again using file.readlines() But returned an empty list

print('len:',len(file.readlines()))
print(file.readlines())
print('len:',len(file.readlines()))

therefore , The proper method of use should be shilling contents=file.readlines(), All subsequent operations are correct contents Conduct .


The official code is as follows :

student.py—— Be responsible for keeping a single piece of student information

'''
Responsible for keeping a single piece of student information
'''
class StudentInfo(object):
def __init__(self, stu_num, stu_name, stu_sex, stu_age):
self.stu_num = stu_num
self.stu_name = stu_name
self.stu_sex = stu_sex
self.stu_age = stu_age
def __str__(self): # The return value must be ( empty ) character string
return self.stu_num.ljust(14) + self.stu_name.ljust(7) + self.stu_sex.ljust(3) + self.stu_age.ljust(3)
# test
if __name__ == '__main__': # If passive operation ,__name__== Module name
stu = StudentInfo('311166000781', ' Master Li ', ' male ', '21')
print(stu)

student_manage.py—— Responsible for managing student information , Operate the student information

'''
Responsible for managing student information , Operate the student information
'''
from student import StudentInfo
import sys
class StudentManage(object):
def __init__(self):
self.students = {} # Save student information
def start(self): # Must be defined as a public method , Otherwise, it cannot be called externally
while True:
self.__showMenu()
select_id = input(" Please enter a number :")
n = int(select_id)
if n <= 8 and n >= 0:
self.__selectFun(select_id)
else:
print(' Please re-enter a valid number !')
# Define private methods
def __showMenu(self):
print('*' * 30)
print('1. Show student information ')
print('2. Search for student information ')
print('3. Add student information ')
print('4. Modify student information ')
print('5. Statistics of student information ')
print('6. Delete student information ')
print('7. Write student information to file ')
print('8. Read in the student information from the file ')
print('0. Exit the system ')
print('*' * 30)
# Select function
def __selectFun(self, select_id):
select_fun = {'1': self.__showAllStu,
'2': self.__searchStu,
'3': self.__addStu,
'4': self.__modifyStu,
'5': self.__countStu,
'6': self.__delStu,
'7': self.__writeStu,
'8': self.__readStu,
'0': sys.exit}
if select_id == '2' or select_id == '4' or select_id == '6':
stu_num = input(' Please enter the student number :')
select_fun[select_id](stu_num)
else:
select_fun[select_id]()
# Add student information
def __addStu(self):
stu_num = input(' Please enter the student number :')
stu_name = input(' Please enter a name :')
stu_sex = input(' Please enter gender :')
stu_age = input(' Please enter age :')
stu = StudentInfo(stu_num, stu_name, stu_sex, stu_age)
self.students[stu_num] = stu
# Search for student information
def __searchStu(self, stu_num):
for k in self.students:
if k == stu_num:
self.__showStu(k)
return k
else:
print(' Check no one !')
return None
# Modify student information
def __modifyStu(self, stu_num):
print(' Looking for >>>')
k = self.__searchStu(stu_num)
if k:
stu = self.students[k]
n = input(' Please enter the modified student number ( There is no need to modify the input -1):')
stu.stu_num = self.__modifyJudge(n, stu.stu_num)
n = input(' Please enter the modified name ( There is no need to modify the input -1):')
stu.stu_name = self.__modifyJudge(n, stu.stu_name)
n = input(' Please enter the modified gender ( There is no need to modify the input -1):')
stu.stu_sex = self.__modifyJudge(n, stu.stu_sex)
n = input(' Please enter the modified age ( There is no need to modify the input -1):')
stu.stu_age = self.__modifyJudge(n, stu.stu_age)
print(' The modification is complete !')
# Modify auxiliary judgment
def __modifyJudge(self, n, value):
if n == '-1':
return value
else:
return n
# Statistics of student information
def __countStu(self):
male = 0
female = 0
age = {}
for v in self.students.values():
if v.stu_sex == ' male ':
male += 1
else:
female += 1
if v.stu_age in age:
age[v.stu_age] += 1
else:
age[v.stu_age] = 1
print(f' Shared by students {len(self.students)} name , Among them, boys have {male} name , Girls share {female} name ')
for k, v in age.items():
print(f'| Age is {k} My classmates have {v} name |')
# Delete student information
def __delStu(self, stu_num):
k = self.__searchStu(stu_num)
if k:
self.students.pop(k)
print(' Delete successful !')
# Display a single message
def __showStu(self, stu_num):
print('| ' + str(self.students[stu_num]) + ' |')
# Show all student information
def __showAllStu(self):
if self.students:
for k in self.students:
self.__showStu(k)
else:
print(' There is no student information at present , Please add ...')
# Write student information to file
def __writeStu(self):
if not self.students:
print(' Student information has not been entered ! Please enter it first and try again ')
else:
file = open('students.txt', 'w')
for stu in self.students.values():
file.writelines(str(stu) + '\n')
file.close()
print(' Write completed !')
# Read in the student information from the file
def __readStu(self):
file = None
try:
file = open('students.txt', 'r')
except Exception as e:
print(' file does not exist , Please check and try again ', e)
else:
contents=file.readlines()
i = 1
for content in contents:
content = content.split()
stu = StudentInfo(content[0], content[1], content[2], content[3]) # Instantiate an object
self.students[content[0]] = stu # Add to the dictionary where student information is stored
print(f' The first {i} Students' information has been entered >>>')
i+=1
print(f'{len(contents)} Student information is entered from the file !')
finally:
if file != None:
file.close()

main.py—— Program entry file

'''
Main program file , Implement the entry of the program
'''
from student_manage import *
if __name__ == '__main__':
stu = StudentManage()
stu.start()


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved