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 6944355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:17:47+00:00 2026-05-27T13:17:47+00:00

I am trying to end a while loop if a condition is not met.

  • 0

I am trying to end a while loop if a condition is not met.

The purpose of this code is to get the most out of a multiple subjects without going over the maximum hours of work a student is willing to put in.

I have created a dictionary called “subjects” which maps out a certain subject to (value, work) with value as how valuable the subject is, and work as how much work is needed to put into that subject. I am adding up the values of the subjects without going over the maximum hours the student is willing to put in. The subjects that make the most sense are then put into a different dictionary.

Here is the code:

def greedyAdvisor(subjects, maxWork, comparator):
"""
Returns a dictionary mapping subject name to (value, work) which includes
subjects selected by the algorithm, such that the total work of subjects in
the dictionary is not greater than maxWork.  The subjects are chosen using
a greedy algorithm.  The subjects dictionary should not be mutated.

subjects: dictionary mapping subject name to (value, work)
maxWork: int >= 0
comparator: function taking two tuples and returning a bool
returns: dictionary mapping subject name to (value, work)
"""

bestVal = {}
tempVal = 0
high = 0
count = 0
tempDict = {}
tempWork = 0
currentBest = None
done = False

while done == False:

    for k in range(len(subjects)+1):
        for i in subjects:
            for j in subjects:
                if i not in bestVal:
                    sub1 = subjects[i][0]
                    sub2 = subjects[j][0]
                    work1 = subjects[i][1]
                    work2 = subjects[j][1]
                    if tempWork >= maxWork:
                            print('tempWork is', tempWork)                         
                            print('bestVal is', bestVal)
                            print('high is', high)
                            print('tempVal is', tempVal)
                            print()
                            return
                    print('sub1 is', sub1)
                    print('sub2 is', sub2)
                    print('work1 is', work1)
                    print('work2 is', work2)
                    maxVal = comparator(sub1, sub2)
                    print('count is', count)
                    count += 1
                    if maxVal == True:
                        print('sub1+tempVal is', sub1+tempVal)
                        print('tempVal is', tempVal)
                        print()
                        if work1 + tempWork > tempWork and tempWork + work1 <= maxWork:
                                high += tempVal+sub1
                                tempWork += work1
                                tempVal = sub1 +tempVal
                                print('sub1', sub1)
                                print('work1 is', work1)
                                print('tempWork is', tempWork)
                                print('tempVal is', tempVal)
                                print('tempWork is', tempWork)
                                bestVal[i] = subjects[i]
                                print('bestVal is', bestVal)
                                print()
                        else:
                            break

The loop ends if maxWork is met, which I have in the code already. The problem is, if maxWork is not met after going through all the subjects, it will continue to loop forever. I need to end the loop after all the items in the dictionary have looped and the condition is not met. I’m guessing I need an “if” statement here, but I just don’t know how to write it. “If all the subjects have been tested and maxWork > tempWork: done = True”

Any help is greatly appreciated.

Thanks

  • 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-05-27T13:17:47+00:00Added an answer on May 27, 2026 at 1:17 pm

    You may just set done = True after all loops.

    done = False
    
    while not done:
        # your bunch of loops code here
        done = True
    

    Further more. Are you really need the while loop? I cannot understand your code completely as it is too much to read but I see only for loops inside while and it looks like other things never change.

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

Sidebar

Related Questions

I'm trying to get out of a while loop when the input is this:
Our application uses a SQL Server back-end with many stored procedures. Recently, while trying
I'm trying to do this in a way that will prevent end-user browser security
I'm trying out several exercises from a Java programming book. I have the code
I am trying to run a stored procedure with a while loop in it
I'm trying to come up with a simple example of a while loop using
Ok, right now I trying to get a list to echo out from mysql
I'm trying to loop through a list of files to get the path and
im trying to figure out how i can do this. i have a list
I am trying to construct an sql query using a while loop that increments

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.