Posted on 

Build a student dormitory management system with Python

Design a student dormitory management system using Python.

Function

  1. Student information: Student ID, name, gender (male/female), dormitory room number, contact phone number.

  2. System functions

    1. The specific information of a student can be found by the student number

    2. New student information can be recorded

    3. All existing student information can be displayed

Analysis

To realize the three functions of dormitory management program.

Added features include the ability to use student names for lookups.

Error handling: When selecting functions, entering student ID, name, gender, dorm room ID, and contact phone number, the data format may be incorrect, and the user needs to input it again. If the search fails, you need to provide the search failure information to the user.

Modularization

A main function and 3 modular function

  1. search_stu You can find the specific information of a student by student number and name.
  2. add_stu Input new student information.
  3. show_all_students Display all existing student information.
  4. main Choose a function.

Code

search_stu

def search_stu():
"按照学号或姓名查找某一位学生的具体信息"
find = -1
t = PrettyTable(["学号","姓名","性别","宿舍房间号","联系电话"])
sea = input("请输入要搜索的学号或姓名: ")
if sea.isdigit() == True:
for i in range(len(stu_info)):
if stu_info[i][0] == sea:
find = i
t.add_row(stu_info[i])
if sea.isalpha() == True:
count = 0
for i in range(len(stu_info)):
if stu_info[i][1] == sea:
find = i
t.add_row(stu_info[i])
if find == -1:
print("抱歉,未查找到该学生。")
else:
print(t)

add_stu

def add_stu():
"录入新的学生信息"
print("-"*50)
print("新增学生")
num = input("请输入学号: ")
while num.isdigit() != True:
num = input("输入错误,请重新输入: ")
name = input("请输入姓名: ")
while name.isalpha() != True:
name = input("输入错误,请重新输入: ")
sex = input("请输入性别:(男/女) ")
while sex != "男" and sex != "女":
sex = input("输入错误,请重新输入: ")
room_no = input("请输入房间号: ")
while room_no.isdigit() != True:
room_no = input("输入错误,请重新输入: ")
tel = input("请输入电话:")
while tel.isdigit() != True:
tel = input("输入错误,请重新输入: ")
stu = [num, name, sex, room_no, tel]
stu_info.append(stu)
print("添加"+num+"成功")

show_all_students

def show_all_students():
for i in range(len(stu_info)):
table.add_row(stu_info[i])
print(table)

Main function

def main():
while True:
print("*"*50)
print("欢迎使用【宿舍管理系统】")
print("1.查找学生")
print("2.新增学生")
print("3.显示全部")
print("0.退出系统")
print("*"*50)
print()
instruct = input("请选择希望执行的操作:")
if instruct == "1":
search_stu()
elif instruct == "2":
add_stu()
elif instruct == "3":
show_all_students()
elif instruct == "0":
print("欢迎再次使用【宿舍管理系统】")
break
else:
print("输入错误,请重新输入指令!")

Test

Input students information

Display all students information

Search by student ID or name

  1. search by id

  2. search by name

Tips: We can find all the students with the same name.

Log out

Robust validation

指令选择错误

信息录入错误

查找错误