我有一个while循环:
def setWorkDays(dayNameList):
workDays = []
while self.count > 0: #continue loop until all 5 work days have been filled or the loop breaks at the end
for day in dayNameList: #iterate over days, adding days to work days if they are not free days, AL days, preferred days, or free Saturdays
if day in self.freeDays or day in self.alDays or (day == 'Saturday' and self.satOff is True):
continue
elif day in self.programDays:
workDays.append(day)
self.count -= 1
elif self.preferredDay is not None and day in self.preferredDay:
continue
else:
workDays.append(day)
self.count -= 1
if self.preferredDay not in self.workDays: #if iteration completes, 5 work days have not been filled, and the preferred day has not been added, add the preferred day
workDays.append(self.preferredDay)
self.count -=1
return workDays
循环背后的想法是 self.count 达到 0 的第二个,循环终止。这是修改 self.count 的唯一函数。然而我得到了奇怪的结果,循环似乎持续了至少 1 个计数太长,因为程序在某些情况下为 self.count 输出 -1。这应该发生吗?while 循环不应该终止第二个 self.count 达到零,还是必须先完成 for 循环?我是否应该在 self.count 递减后添加条件逻辑来检查 self.count 是否为零,如果为零则中断?这似乎是while循环的目的......