如果条件不满足,如何结束while循环

0 投票
3 回答
4377 浏览
提问于 2025-04-17 08:27

我想在一个循环中,如果条件不满足,就结束这个循环。

这段代码的目的是为了在多个科目中尽量获取最大的收益,同时不超过学生愿意投入的最大工作时间。

我创建了一个叫“subjects”的字典,这个字典把每个科目和它的(价值,工作量)对应起来。这里的“价值”表示这个科目的重要性,而“工作量”则是学习这个科目需要投入的时间。我在尽量不超过学生愿意投入的最大工作时间的情况下,累加这些科目的价值。最后,把最有意义的科目放到另一个字典里。

以下是代码:

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

这个循环会在达到最大工作时间时结束,这个我已经在代码里写好了。问题是,如果在遍历完所有科目后,最大工作时间还没有达到,循环就会一直进行下去,变成死循环。我需要在遍历完字典里的所有项目后,如果条件不满足,就结束这个循环。我猜我需要在这里加一个“if”语句,但我不知道该怎么写。“如果所有科目都已经测试过,并且最大工作时间大于临时工作时间:done = True”

任何帮助都非常感谢。

谢谢

3 个回答

1

你写的代码有点多了!

你可以考虑把代码的工作分开来做:首先把科目按好坏排序(可以用 sorted 函数,它有一个 cmp 参数),然后遍历这个排序好的列表,把它们加到一个结果变量里,直到你要超过最大工作量为止。这样你就不会遇到现在的问题,因为你自然会在遍历完排序好的列表后停止。

通常,把你要做的事情分成几个逻辑上独立的部分(比如这里先排序,再汇总结果)会让你的代码更简单,也更容易理解。

3

在最后一个 for 循环后面加上 done = True 应该就够了,但你的代码还有其他问题。

那个 while 循环完全没必要。如果你把它去掉,代码应该就能按预期工作了。

你还有三个循环是根据科目的数量来写的,所以你的总循环次数(去掉 while 后)会是科目数量的立方。也就是说,如果你有100个科目,最里面的部分如果没有找到匹配,就得执行1000000次。我真的看不出这个“k”循环有什么用。你似乎并没有对这个值做什么,所以它只是无意义地重复了内部的循环。

0

你可以在所有循环结束后,把 done = True 这行代码放在最后。

done = False

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

另外,你真的需要这个 while 循环吗?我没法完全理解你的代码,因为内容太多了,不过我只看到在 while 循环里面有 for 循环,看起来其他的东西好像没有变化。

撰写回答