#-*- coding:utf-8 -*- import random room = [[],[],[],[]] teacher = ["张老师","章老师","赵老师","李老师","王老师","昌老师","刘老师","陈老师","汪老师","秦老师"] #先随机选出4个老师放到教室,保证每个房间2人,循环2次 i= 0 while i < 2: for tempRom in room: index = random.randint(0,len(teacher)-1) #利用teacher的下标生成1个随机数 tempTeac = teacher.pop(index) #利用pop删除随机的下标老师,并赋值给tempTeac tempRom.append(tempTeac) #在房间里添加刚才删除的老师 i+=1 #把剩下的老师进行分配 for teac in teacher: index2 = random.randint(0,len(room)-1)#随机生成教室的下标 room[index2].append(teac) #把老师添加到随机的教室 #遍历房间,把每个教室的老师打印出来 j = 1 for temp in room: print("-"*20) print("第%d个教室的老师:"%j) for temp2 in temp: print(temp2) j+=1