我正在編寫一個程序來隨機生成辦公室尋寶團隊的程序,因此我正在組織所以我編寫了這個簡單的代碼來模擬從帽子中挑選姓名,但只有更公平,但不完全知道爲什麼它不工作,它不返回所有的名字,比如我輸入了6名進入名單,但它只返回其中的4個這樣的:通過列表中的值隨機生成一組人羣
Group 1 consists of;
Chris
Ryan
Paul
Group 2 consists of;
Alex
我不在清除列表中的元素方面有很多經驗,所以它可能只是我做錯了。任何見解都會有所幫助。
import random
participants=["Alex","Elsie","Elise","Kimani","Ryan","Chris","Paul"]
group=1
membersInGroup=3
for participant in participants:
if membersInGroup==3:
print("Group {} consists of;".format(group))
membersInGroup=0
group+=1
person=random.choice(participants)
print(person)
membersInGroup+=1
participants.remove(str(person))
的可能的複製[德爾的區別,刪除和彈出列表上(http://stackoverflow.com/questions/11520492/del-remove-and-pop-on-lists之間的差異) –