当前位置:   article > 正文

【python习题】随机分配_python随机分配

python随机分配

老师随机分配办公室

#8个老师随机放到3个办公室

import random   #随机数生成

offices = [[],[],[]]   #三个办公室
names = ['A','B','C','D','E','F','G','H']   #8个老师
for name in names:
    index = random.randint(0,2)   #老师随机分配三个列表,索引为0,1,2
    offices[index].append(name)   #将老师名字加入列表中
i = 1
for office in offices:
    print("办公室%d的人数为:%d"%(i,len(office)))   #输出办公室(1开始),所容纳人数
    i += 1
    for name in office:
        print("%s"%name,end='\t')
    print('\n')
    print("-"*10)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/人工智能uu/article/detail/851578
推荐阅读
相关标签
  

闽ICP备14008679号