Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9104431
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T01:52:02+00:00 2026-06-17T01:52:02+00:00

I have a while loop: def setWorkDays(dayNameList): workDays = [] while self.count > 0:

  • 0

I have a while loop:

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

the idea behind the loop is that the second that self.count hits 0, the loop is terminated. This is the only function in which self.count is modified. Yet I’m getting strange results, where the loop appears to go on for at least 1 count too long, as the program is outputting -1 in some cases for self.count. Should this be happening? Shouldn’t the while loop terminate the second self.count hits zero, or does it have to first finish the for loop? Should I be adding conditional logic after the self.count decrements that checks if self.count is zero and breaks if it is? That seems like the purpose of the while loop …

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T01:52:03+00:00Added an answer on June 17, 2026 at 1:52 am

    A while loop doesn’t automatically exit in mid-loop as soon as its condition is no longer true; it just checks the condition at the start of each loop. If you want to get out early, you need to break explicitly (or do something else non-local, like return from the function or raise to an except handler outside the loop).

    It seems like what you’re trying to do is get out of the for loop early, if self.count ever hits 0. There’s really no way to do that directly. You have to check each time you decrement it.

    However, you really don’t need self.count at all. You decrement it exactly in the same places you append to workDays. So, just check whether you’ve got 5 of them yet. In other words, each self.count -= 1 becomes:

    if len(workDays) >= 5: break
    

    There is actually a way to do what (I think) you want in Python: use a generator instead of a list. If you yield each value instead of appending it to a list and then returning that list at the end, then you just stop iterating over the generator once you get 5 entries.

    For example:

    def setWorkDays(dayNameList):
        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:
                    yield day
                elif self.preferredDay is not None and day in self.preferredDay:
                    continue
                else:
                    yield day
            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 
                yield day
    
    workDays = [setWorkDays(dayNameList) for _ in range(5)]
    

    Often, you don’t even really need the list, all you need to do is iterate over it. For that, you could do:

    for workDay in (setWorkDays(dayNameList) for _ in range(5)):
    

    Or:

    for workDay in itertools.islice(setWorkDays(dayNameList), 5):
    

    A lot of things that generators can do feel like magic until you understand them—which often means you shouldn’t do them until you learn about generators. So, if this makes no sense to you, don’t just pick it up and use it. But if it prompts you to learn how to write and use generator functions, great!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The while loop I have while reading in from a file doesn't break. I'm
I have a while loop that goes while a BuffedReader still has data, what
I have a while loop that loops through 3 results and echo's these out
hi i have a while loop: var i = 0; while(i < 20) {
So with pygame you have a while loop that loops continuously, then your event
Currently, I have a while loop for listening the port. When a socket accepta
I have a script that uses a simple while loop to display a progress
I have a tabled view in a while loop, where a user can view
I have some problems with controlling a while loop inside an event structure. Say
I have a function creating entries via grid() like this: (replace while-loop with function,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.