如果未满足条件,则结束While循环

时间:2011-12-15 17:20:13

标签: python

如果条件不满足,我试图结束一个while循环。

此代码的目的是在不超过学生愿意投入的最长工作时间的情况下,充分利用多个科目。

我创建了一个名为“科目”的词典,它将某个科目映射到(价值,工作)的价值作为科目的价值,并且需要投入多少工作才能投入到该科目中。我在不超过学生愿意投入的最长时间内将学科的价值加起来。然后将最有意义的主题放入不同的词典中。

以下是代码:

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

如果满足maxWork,则循环结束,我已经在代码中找到了。问题是,如果在经过所有主题后没有达到maxWork,它将继续循环。我需要在字典中的所有项目循环并且不满足条件后结束循环。我猜这里需要一个“if”语句,但我只是不知道如何编写它。 “如果所有主题都经过测试并且maxWork&gt; tempWork:done = True”

非常感谢任何帮助。

由于

3 个答案:

答案 0 :(得分:3)

在上一个done = True循环之后添加for就足够了,但您的代码还有其他问题。

while循环完全没必要。如果删除它,代码应该可以正常工作。

根据主题数量,您还有三个循环,因此您的总迭代次数(与while一起)将成为立方数的主题。这意味着如果你有100个主题,如果找不到匹配,最里面的部分必须执行100万次。我真的没有看到“k”循环的目的。你似乎没有对那个值做任何事情,所以它只是在非生产性地重复内部循环。

答案 1 :(得分:1)

你写的代码太多了!

您可能想要分割代码的工作方式:首先按照良好的顺序对主题进行排序(可能使用带有sorted参数的cmp函数),然后通过排序列表将它们添加到当你超过maxWork时结果变量停止。您将不会遇到您当前遇到的终止问题,因为一旦您完成了排序列表,您将自然停止。

通常,将您正在进行的操作分解为逻辑上单独的位(此处,先排序然后再将结果聚合在一起),使您更容易理解代码。

答案 2 :(得分:0)

您可以在所有循环后设置done = True

done = False

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

更多。你真的需要while循环吗?我完全无法理解你的代码,因为它太多了,但我只看到for内的while循环,看起来其他东西永远不会改变。

相关问题