Python basic tutorial - making a dormitory management system (full version, source code attached)

Today we will study a new small case - dormitory management system. It mainly involves the initialization, addition, deletion, modification and query operations of lists and dictionaries, as well as the definition and calling of functions.

1. Requirements:

  1. There is an operation guidance interface that displays the operation number.

  2. A new student information can be added, including student name, dormitory number + bed number (such as 313-3), class, and check-in status (on campus or on leave)

  3. You can delete a student's accommodation information. Enter the name to query. If it exists, it will be deleted and it will prompt that it has been deleted. If it does not exist, it will prompt that the student's information does not exist in the system.

  4. You can modify a student's accommodation information, enter the name to query, if it exists, the user will be asked to re-enter the modification information, and it will prompt that it has been modified. If it does not exist, it will prompt that the system does not exist for the student's information.

  5. You can query the accommodation information of a student by entering the name. If it exists, the student's accommodation information will be given. If it does not exist, it will prompt that the system does not exist for the student's information.

  6. Can display accommodation information for all students

  7. Can display the accommodation information of all students who are on leave

  8. Can exit the system

    Insert image description here

2. Code writing

1. Define a global variable list to store all student information

# 全局变量用来存储所有学生信息
student_infors = []

2. Define software prompt interface functions

def Directory():
#学生宿舍管理系统 V1.0
# 1打印功能提示
print('=' * 50)
print('学生宿舍管理系统 V1.0')
print('1:添加一个新的入住学生信息')
print('2:删除一个学生的住宿信息')
print('3:修改一个学生的住宿信息')
print('4:查询一个学生的住宿信息')
print('5:显示所有的学生的住宿信息')
print('6.显示所有请假学生的信息')
print('7:退出系统')
print('=' * 50)

3. Define and add a student accommodation information function

#添加学生信息函数
def  Add_infor():
# 定义一个新的字典,用来存储一个新的学生信息
new_infor = {
    
    }
new_infor['student_name'] = input('请输入新入住的学生名字:')
new_infor['D_num'] = input('请输入宿舍号+床位号(如313-3):')
new_infor['Class_num'] = input('请输入班级:')
new_infor['status'] = input('请输入入住情况(在校or请假):')
# 将一个字典,添加到列表中
student_infors.append(new_infor)

4. Define a function to delete a student’s accommodation information

#删除学生信息函数
def Delete_infor():
del_name = input("请输入要删除的退宿学生名字:")
find_flag = False
for line in student_infors:
if line['student_name'] == del_name:
find_flag = True
student_infors.remove(line)
break
if find_flag:
print("已删除!")
else:
print("系统不存在该学生的信息!")

5. Define a function to modify a student’s accommodation information

# 修改某个学生的信息
def Update_one_infor():
old_name = input('请输入要修改的学生姓名:')
flag = 0
for line in student_infors:
if line['student_name'] == old_name:
line['student_name'] = input('请输入需要修改的学生名字:')
line['D_num'] = input('请输入需要修改的宿舍号+床位号(如313-3):')
line['Class_num'] = input('请输入需要修改的班级:')
line['status'] = input('请输入需要修改的入住情况(在校or请假):')
flag = True
break
if flag:
print("已修改!")
else:
print('系统不存在该学生的信息!')

6. Define a function to query the accommodation information of a student

# 查找某个学生的信息
def Find_one_infor():
find_nmae = input("请输入要查找的学生姓名:")
find_flag = 0  # 默认表示没有找到
for temp in student_infors:
if find_nmae == temp['student_name']:
print ('学生名字\t宿舍号+床位号\t班级\t\t入住情况')
print('%s\t\t%s\t\t%s\t\t%s' % (temp['student_name'], temp['D_num'], temp['Class_num'], temp['status']))
find_flag = 1  # 表示找到了
break
# 判断是否找到
if find_flag == 0:
print('系统不存在该学生的信息')

7. Define a function that displays all student accommodation information

#查找所有入住学生信息
def Find_all_infor():
print ('学生名字\t宿舍号+床位号\t班级\t\t入住情况')
for temp in student_infors:
print('%s\t\t%s\t\t%s\t\t%s' % (temp['student_name'], temp['D_num'], temp['Class_num'], temp['status']))

8. Define a function to display the accommodation information of all students on leave

def Find_leave ():
leave_flag = 0  # 默认表示没有找到
print ('学生名字\t宿舍号+床位号\t班级\t\t入住情况')
for temp in student_infors:
if temp['status']== '请假' :
print('%s\t\t%s\t\t%s\t\t%s' % (temp['student_name'], temp['D_num'], temp['Class_num'], temp['status']))
leave_flag = 1  # 表示找到了
# 判断是否找到
if leave_flag == 0:
print('系统不存在请假学生')

9. Define a main function, organize and call functional functions according to the software process requirements, and realize the functions of the entire system.

def main():
Directory()
while True:
# 2获取用户选择
num = input('请输入操作序号:')
if num.isdigit():
num = int(num)
if num == 1:
print('1:添加一个新的入住学生信息')
Add_infor()
elif num == 2:
print('2:删除一个学生住宿信息')
Delete_infor()
elif num == 3:
print('3:修改一个学生住宿信息')
Update_one_infor()
elif num == 4:
print('4:查询一个学生住宿信息')
Find_one_infor()
elif num == 5:
print('5:显示所有的学生住宿信息')
print()
Find_all_infor()
elif num == 6:
print('6:显示所有请假学生信息')
Find_leave()
elif num == 7:
print("已退出系统!")
break
else:
print('输入有误!目前只有1-7项功能哦')
continue
print('')
else:
print("输入错误,请重新输入!1-7")

10. The top grid calls the main function and starts the software

main()

A dormitory management system is ready!

Expansion: You can try to use tinker to create an interface for the software. You can also consider using pandas to operate Excel files to read, write and store data. Or use a database connection.

Friends who have read the case, please click and scan the code to help me follow it. Your support is the motivation for me to continue to promote new cases.

Finally hereFree share with you a Python learning material, including videos and source code. I hope this courseware can help those friends who are dissatisfied with the current situation and want to improve themselves but have no direction. They can also learn and communicate with me.
Programming materials, learning roadmap, source code, software installation package, etc.!

Look at the picture below (dropped)↓↓↓

Python所有方向的学习路线图, know what to learn in each direction
100多节Python课程视频, cover the necessary basics, crawlers and Data analysis
100多个Python实战案例, learning is no longer just theory
华为出品独家Python漫画教程, mobile phone Can also learn
历年互联网企业Python面试真题, very convenient for review****

Insert image description here

Guess you like

Origin blog.csdn.net/2301_78217634/article/details/135034884