# 一个学校有3个办公室,现在有8位老师,请给老师们随机分配工位 # 定义学校和办公室
import randomschool = [[],[],[]] # 定义老师列表 teacher_list = [] index = 1 while index <= 8: # 创建老师的名字 teacher_name = '老师' + str(index) # 将老师名称放入老师列表中 teacher_list.append(teacher_name) index = index + 1 print(teacher_list) # ********************************************************** # 将定义老师写为函数 def creat_teschers(): # 定义老师列表 teacher_list = [] index = 1 while index <= 8: # 创建老师的名字 teacher_name = '老师' + str(index) # 将老师名称放入老师列表中 teacher_list.append(teacher_name) index = index + 1 return teacher_list teachers_list = creat_teschers() print(teachers_list) # 查看变量id print(id(teachers_list)) # 分配老师 for teacher in teachers_list: # 获得随机办公室 office_num = random.randint(0,2) # 给老师随机分配办公室 school[office_num].append(teacher) # 查看各个办公室的老师 for office in school: for person in office: print(person,end=' ') # 换行 print()
python基础案例练习二
最新推荐文章于 2023-03-04 22:11:09 发布