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

Using Python to realize a simple student information management system -- 2.0

編輯:Python

This time, compared with the last time, the change is not very big , Login added 、 Registration function , There are also data written directly to the file while adding ; When judging the password , Regular expressions are used to judge .

The modules divided this time :

1.main.py

This run main When the method is used , First enter login , The registration screen

# Import the management system module
from managerSystem import *
# if conditional , Ensure that the current program runs before running the management system
if __name__ == "__main__":
# Create instance object
manager_user = StudentManager()
manager_user.register()

2.student.py and user.py

These two modules are two classes abstracted ,student and user, And define some corresponding attributes

# Student class
class Student():
# Initialize magic methods
def __init__(self, name, gender, tel):
self.name = name
self.gender = gender
self.tel = tel
# “ magic ” Method , When using print When outputting objects, you only need to define them __str__(self) Method ,
# Then it will print from in this method return The data of .
def __str__(self):
return f" full name :{self.name}, Gender :{self.gender}, Telephone :{self.tel}"
# t = Student("aa"," Woman ",23)
# print(t)
class Unser():
def __init__(self, username, password):
self.username = username
self.password = password
def __str__(self):
# “ magic ” Method , When using print When outputting objects, you only need to define them __str__(self) Method ,
# Then it will print from in this method return The data of .
return f" user name :{self.username}, password :{self.password}"
# u = Unser("ss",123)
# print(u)

3.managerSystem.py

The code is as follows :

# Main interface code
# Import user,student modular
from student import *
from user import *
# When using regular expressions , Need to introduce re library
import re
class StudentManager():
def __init__(self):
# initialization , Create an empty list
self.student_list = []
self.user_list = []
def register(self):
print(" Welcome to the login interface ")
while True:
print(" Please enter the operation you want to do : a/ register ,b/ Sign in ")
register_way = input(" Please enter :")
if register_way == "a":
# register
username = input(" Please enter a user name :")
password = input(" Please input a password :")
# Let's judge the password through regular expressions
ret = re.match(r"^[123456789]\d{5}$", password)
if ret:
user = Unser(username, password)
self.user_list.append(user)
# To verify
print(" The user you registered is :")
print(self.user_list)
print(user)
print(" Confirm registration :Y/ yes ,N/ no ")
confirm = input(" Please enter :")
if confirm == "Y":
self.save_user()
elif confirm == "N":
print(" Not saved ")
else:
print(" You entered it wrong ")
else:
print(" The password you entered does not meet the specification , Please re register ")
# Sign in
elif register_way == "b":
# Load user information
self.load_user()
register_username = input(" Please enter your account number ")
register_password = input(" Please input a password ")
# Traverse the list
for i in self.user_list:
if i.username == register_username and i.password == register_password:
# Login successful
# Create instance object , call run() Method
student_manager = StudentManager()
student_manager.run()
print(" Wrong user name or password , Please login again .")
else:
print(" Your choice is wrong ,")
# Define program entry functions
# 1. Load data
# 2. Function menu
# 3. Enter the function serial number
# 4. Perform different functions
def run(self):
# Load student information
self.load_student()
while True:
# This is to call the instance method in the class , Call the function menu function
self.show_menu()
menu_num = int(input(" Please enter the function number :"))
if menu_num == 1:
# Add students
self.add_student()
elif menu_num == 2:
# Delete student
self.del_student()
elif menu_num == 3:
# Modify student information
self.modify_student()
elif menu_num == 4:
# Query student information
self.search_student()
elif menu_num == 5:
# Display all student information
self.show_student()
elif menu_num == 6:
# Save student information
self.save_student()
elif menu_num == 7:
# sign out
break
# System function
# Function menu
# Objects and object data are not involved , Define static methods
@staticmethod
def show_menu():
print("------------------- Welcome to the student information management system -------------")
print(" Please select the function below ")
print("1. Add students ")
print("2. Delete student ")
print("3. Modify student information ")
print("4. Query student information ")
print("5. Show all students ")
# print("6. Save student information ")
print("7. Exit the system ")
# Define instance properties , Use a list to store information about each student
# Add students
def add_student(self):
# User input , full name , Gender , cell-phone number
name = input(" Please enter your name :")
gender = input(" Please enter your gender :")
if gender == " male " or gender == " Woman ":
tel = input(" Please input your mobile number /6 Integer digits :")
# utilize try Statement can be used to judge whether the segment phone number is 6 An integer
try:
tel_number = int(tel)
except:
print(" The mobile number you entered is incorrect , Please do it again ")
else:
tel_len = len(tel)
if tel_len == 6:
# Create student object , Import first Student class , Import first student modular
student = Student(name, gender, tel)
# Store student data in the list
self.student_list.append(student)
# Check it out
print(self.student_list)
print(" The information you added is :")
print(student)
else:
print(" The phone number you entered is wrong , Please do it again ")
else:
print(" The gender you entered is incorrect , Please do it again ")
confirm = input(" Are you sure :Y/ yes ,N/ no ")
if confirm == "Y":
self.save_student()
elif confirm == "N":
print(" Not saved ")
else:
print(" You entered it wrong ")
# tel = input(" Please input your mobile number /6 Integer digits :")
# Delete student
def del_student(self):
# Delete students by name
del_name = input(" Please enter the name of the student to delete ")
# Traverse the list , lookup
for i in self.student_list:
if del_name == i.name:
# Delete student object
self.student_list.remove(i)
print(" Delete successful ")
# Can't find , No information found in printing
else:
print(" This student information is not found ")
# Print the list again , See if you want to delete
print(self.student_list)
# Modify student
def modify_student(self):
# Enter the name of the target student
modify_name = input(" Please enter the student information you want to modify :")
# Traverse the list to find whether it exists , It was modified , If there is no information, it will prompt that there is no information
for i in self.student_list:
if modify_name == i.name:
# Update student information
i.name = input(" Please enter the student name :")
i.gender = input(" Please enter student gender :")
i.tel = input(" Please enter the student telephone number :")
print(f" The revised student information is : full name :{i.name}, Gender :{i.gender}, Telephone :{i.tel}")
break
else:
print(" There is no student information ")
# Query student information ( Search by name or phone )
def search_student(self):
print(" A query a/ Name inquiry ,b/ Telephone inquiry ")
search_way = input(" Please enter :")
if search_way == "a":
search_name = input(" Please enter the names of all your queries :")
for i in self.student_list:
if i.name == search_name:
print(f" The student information is : full name :{i.name}, Gender :{i.gender}, Telephone :{i.tel}")
break
else:
print(" The student information is not found ")
elif search_way == "b":
search_tel = input(" Please enter the phone number you inquired :")
for i in self.student_list:
if i.tel == search_tel:
print(f" The student information is : full name :{i.name}, Gender :{i.gender}, Telephone :{i.tel}")
break
else:
print(" The student information is not found ")
else:
print(" The information you entered is wrong , Please re-enter ")
# Show all students
def show_student(self):
print(" Student information is as follows :")
# Traverse the output
for i in self.student_list:
print(f" full name :{i.name}, Gender :{i.gender}, Telephone :{i.tel}")
# Save student information
def save_student(self):
# Open file
r = open("student.txt", "w")
# When writing data in the file, first turn the student object in the list into a dictionary , Write the file again
new_student_list = [i.__dict__ for i in self.student_list]
# write in , Then convert the dictionary to a string
r.write(str(new_student_list))
# Line feed function
r.write("\r\n")
# Close file
r.close()
print(" Saved successfully ")
# Load student information
def load_student(self):
# If the file exists, open it in read-only mode , Otherwise, open in write mode
try:
r = open("student.txt", "r")
except:
r = open("student.txt", "w")
else:
# Reading data , The data read out is of string type , First restore it to a dictionary ,
data = r.read()
# eval Function is to implement list、dict、tuple And str Transformation between
new_list = eval(data)
self.student_list = [Student(i["name"], i["gender"], i["tel"]) for i in new_list]
finally:
r.close()
def save_user(self):
# Open file
r = open("user.txt", "w")
# When writing data in the file, first turn the student object in the list into a dictionary , Write the file again
new_user_list = [i.__dict__ for i in self.user_list]
# write in , But first convert the list to a string
# str Function list,dict,tuple Convert to string
r.write(str(new_user_list))
r.write("\r\n")
# Close file
r.close()
print(" Saved successfully ")
# Load user information
def load_user(self):
# If the file exists, open it in read-only mode , Otherwise, open in write mode
try:
r = open("user.txt", "r")
except:
r = open("user.txt", "w")
else:
# Reading data , The data read out is of string type , First restore it to a dictionary ,
data = r.read()
new_user_list = eval(data)
self.user_list = [Unser(i["username"], i["password"]) for i in new_user_list]
finally:
r.close()


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