程序继续运行 - 无限循环

时间:2017-07-22 16:20:09

标签: python infinite-loop

我一直在创建一个组合计算器,这是我很难创造的东西。我不断尝试解决的一个问题是处理代码中的任何无限循环。

oglist = ["a","b","c","d"]
combocounter = 3
lists = {}
comboloop = True
combolist = ["lol"]
pendinglist = ["lol"]
for x in range(0, combocounter):
    lists["list" + str(x)] = ["lol"]
def loop(counter1):
    global recursion1
    global recursion2
    if len(lists["list" + str(counter1)]) == 0:
        lists["list" + str(counter1 - 1)] = lists["list" + str(counter1 - 1)][1:]
        print(lists)
        recursion1 = True
        recursion2 = True
    else:
        lists["list" + str(counter1 + 1)] = lists["list" + str(counter1)][1:]
        print(lists)
        recursion2 = False
    return
def startingloop():
    global recursion1
    if len(lists["list0"]) == 0:
        comboloop = False
    else:
        lists["list1"] = lists["list0"][1:]
        print(lists)
        recursion1 = False
    return
def endingloop():
    global counter2
    global recursion2
    if len(lists["list2"]) == 0:
        lists["list1"] = lists["list1"][1:]
        print(lists)
        recursion2 = True
    else:
        combolist[counter2] = lists["list0"][0]
        for y in range(1, combocounter):
            combolist[counter2] = combolist[counter2] + lists["list" + str(y)][0]
        combolist.append("lol")
        lists["list2"] = lists["list2"][1:]
        counter2 += 1
        print(lists)
        print(combolist)
    return
lists["list0"] = oglist
counter2 = 0
while comboloop == True:
    startingloop()
    while recursion1 == False:
        loop(1)
        while recursion2 == False:
            endingloop()
combolist.remove("lol")
print(combolist)

我已经放置了一堆打印功能: print(lists)print(combolist)。 当我运行它时,列表和组合器会不断更新和打印。然后它停止打印,这是预期的,但我的程序继续运行的东西。它也永远不会到达

combolist.remove("lol")
print(combolist)

我努力遵循代码的逻辑来发现任何问题,但我没有。我的代码中不断循环的是什么?

1 个答案:

答案 0 :(得分:0)

comboloop = False正在创建一个局部变量,它会影响全局调用的comboloop。如果你添加

global comboloop
在你的startingloop()函数中,程序退出