| 
 | 
 
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册  
 
x
 
- '''
 
 - 8位老师,3个办公室,随机分配8位老师到3个办公室
 
 - 1.需要有8位老师的数据
 
 - 2.验证办公室数据
 
  
- 3.[color=Red]每个办公室最起码要一人 ##求思路这个地方该怎么写,谢谢指点[/color]
 
 - '''
 
 - import random
 
 - teachers =['A','B','C','D','E','F','G','H'] #八位老师
 
 - offices =[[],[],[]] #三间教室
 
 - for teacher in teachers:    #先遍历老师的名字
 
 -     num = random.randint(0,2)   #随机分配3间教室
 
 -     offices[num].append(teacher)    #把老师随机添加到三间教室
 
 - i = 1   #办公室的序号
 
 - for office in offices:  #把每个办公室的老师遍历出来
 
 -     print(f'办公室的人数是{len(office)},老师分别是:')
 
 -     for tea in office:  #把每间办公室的名字打印出来
 
 -         print(tea)
 
 -     i += 1
 
  复制代码 
 
求助,如何实现第3步骤,就是保证分配到每个办公室的人数至少是一人 
求助,如何实现第3步骤,就是保证分配到每个办公室的人数至少是一人 
求助,如何实现第3步骤,就是保证分配到每个办公室的人数至少是一人
- import random
 
  
 
- def group():  # 分组
 
 -     offices = office1, office2, office3 = [], [], []
 
 -     for i in range(1,9):
 
 -         random.choice(offices).append(i)
 
 -     return offices
 
  
- def check(offices):  # 验证
 
 -     for each in offices:
 
 -         print(len(each))
 
 -         if len(each) == 0:
 
 -             return True
 
 -     return False
 
  
- while True:
 
 -     offices = group()
 
 -     if check(offices):
 
 -         continue
 
 -     else:
 
 -         break
 
 - print(offices)
 
  复制代码 
 
 
 |   
 
 
 
 |