解决思路:
1 确定团队成员个数num,然后根据成员个数生成元素非重复的数组;
2 构成一个团队成员字典,键:成员名 值:0, 然后将生成的数组分别赋值给字典键对应的值;
话不多说,看代码便知:
# -*- coding: utf-8 -*-
import random
# 团队成员名对应随机数字典
nsdlTeamMember = {'mem1':0, 'mem2':0, 'mem3':0, 'mem4':0, 'mem5':0, 'mem6':0, 'mem7':0, 'mem8':0}
def teamMarking(teamMember):
#存储生成非重复随机数的列表
chooseNumList = []
# 生成成员个数的非重复随机数
for i in range(len(teamMember.keys())):
num = random.randint(1,len(teamMember.keys()))
while num in chooseNumList:
num = random.randint(1,len(teamMember.keys()))
chooseNumList.append(num)
# 将生成的随机数数组赋值给成员键对应的值
print("一共有个%d成员,抓阄结果如下:"% len(teamMember.keys()))
# print("一共有个%d成员,分别是:%s\n抓阄结果如下:" %(len(teamMember.keys()),' '.join(teamMember.keys())))
index = 0
for k,v in teamMember.items():
v = chooseNumList[index]
index += 1
print(k.ljust(8) + str(v).rjust(4))
if __name__ == '__main__':
teamMarking(nsdlTeamMember)
------- 实际运行结果:----------
[cxy@localhost-live python]$ python team_marking.py
一共有个8成员,抓阄结果如下:
mem5 1
mem4 7
mem7 8
mem6 3
mem1 2
mem3 4
mem2 5
mem8 6
[cxy@localhost-live python]$ python team_marking.py
一共有个8成员,抓阄结果如下:
mem5 6
mem4 5
mem7 3
mem6 1
mem1 8
mem3 4
mem2 7
mem8 2
标签:teamMember,python,成员,len,keys,num,团队,抓阄
来源: https://www.cnblogs.com/noxy/p/10391072.html