赞
踩
利用python列表相关知识,根据用户输入的办公室数目count和教师数目num,将num个教师 随机 分配到count个办公室中。
count = eval(input('请输入办公室个数:'))
offices = [[] for j in range(count)]
作用: 构建一个含有count个[]的列表offices,即最终的offices为一个嵌套列表。如用户输入的count=3,则执行过代码之后的offices为 [[],[],[]]
teachers = []
num = eval(input('请输入老师位数:'))
print(f'请依次输入这{num}位老师的名字:')
i= 0
while i < num:
print(f'第{i + 1}位老师名字:',end="\t")
name = input()
teachers.append(name)
i += 1
for name in teachers:
num = random.randint(0, count-1)
offices[num].append(name)
i = 1
for office in offices:
if len(office)==0:
print(f'办公室{i}没有老师')
else:
print(f'办公室{i}的人数是{len(office)},老师分别是:')
for name in office:
print(name)
i += 1
import random teachers = [] count = eval(input('请输入办公室个数:')) offices = [[] for j in range(count)] num = eval(input('请输入老师位数:')) print(f'请依次输入这{num}位老师的名字:') i= 0 while i < num: print(f'第{i + 1}位老师名字:',end="\t") name = input() teachers.append(name) i += 1 for name in teachers: num = random.randint(0, count-1) offices[num].append(name) i = 1 for office in offices: if len(office)==0: print(f'办公室{i}没有老师') else: print(f'办公室{i}的人数是{len(office)},老师分别是:') for name in office: print(name) i += 1
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。