我的代碼中有一個非常奇怪的錯誤。 我試圖創建一個代碼,爲我和我的團隊創建隨機班次,5天內有5個班次,每個人每天都應該有不同班次。它似乎蟒蛇'list.pop()'不工作,因爲它應該
代碼它的小大,所以我會嘗試通過只有相關的作品在這裏,但隨時要求更多的信息,我會嘗試澄清。
from random import randint
shifts = [4, 4.2, 5, 6, 7]
days_names = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday']
workers_names = ['Itai', 'Or', 'Reut', 'Kuka', 'Aviel']
class Day(object):
#create empty shift : worker list
def __init__(self,shift,name):
self.shift_list = { x:'' for x in shift}
self.name = name
# create a list of workers that not work this day and choose
# choose a random worker in 'worker_chosen' function
def shift_arrenge(self):
self.today_workers = [x for x in workers_names if (x not in self.shift_list.values())]
for shift, worker in self.shift_list.items():
if not worker:
self.shift_list[shift] = worker_chosen(shift,self.today_workers,self.name)
print (self.shift_list)
def worker_chosen(shift,WORKERS,day_name):
worker_chosen =''
print("today_workers")
print(WORKERS)
while(WORKERS):
worker_chosen = WORKERS.pop(randint(0, len(WORKERS)-1))
print("worker chosen: "+ worker_chosen)
#check if the worker got this shift already if not update on the worker and return it
if not (workers[worker_chosen].shift_list[shift]):
workers[worker_chosen].update(shift,day_name)
break
return worker_chosen
現在的錯誤是在worker_chosen = WORKERS.pop(randint(0, len(WORKERS)-1))
左右。 如果選擇的工人班次已經被選中並且程序選擇了另一名工人,那麼在下一次我的shift_arrenge
函數調用worker_chosen時。沒有選擇階梯的工作人員從我的工作人員名單中錯過了。它像流行音樂完全移除他。
這裏就是比如我調試輸出:
today_workers
['Itai', 'Or', 'Reut', 'Kuka', 'Aviel']
worker chosen: Or
worker Or didn't got choose..
worker chosen: Reut
worker Reut didn't got choose..
worker chosen: Kuka
worker Kuka got choose!
#now move to the next shift..
today_workers
['Itai', 'Aviel'] # but only kuka got choose!
如果有疑問,責怪Python的'list.pop()'?我認爲我們可以認爲問題在於別處。 :-) –
哪裏是打印'工人沒有得到選擇..'的代碼? –
我在這裏刪除此行。如果if代碼沒有得到執行代碼coem到行'print(「worker」+ worker_chosen +「沒有選擇..」)' –