【问题标题】:Program keeps running - infinite loop程序继续运行 - 无限循环
【发布时间】:2017-07-22 16:20:08
【问题描述】:

我一直在创建一个组合计算器,但我很难创建它。我一直试图解决的一个问题是处理我的代码中的任何无限循环。

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)

我努力按照我的代码逻辑来查找任何问题,但我没有。我的代码中不断循环的内容是什么?

【问题讨论】:

    标签: python infinite-loop


    【解决方案1】:

    comboloop = False 正在创建一个局部变量,它会影响您的全局变量,称为组合循环。如果你添加

    global comboloop
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-26
      • 1970-01-01
      • 2019-02-26
      • 1970-01-01
      • 2022-09-30
      • 1970-01-01
      • 1970-01-01
      • 2012-04-26
      相关资源
      最近更新 更多