[英]Program keeps running - infinite loop
I've been creating a combination calculator, something I'm having a hard time creating. 我一直在创建组合计算器,这在我创建过程中很困难。 A problem I'm constantly trying to fix is dealing with any infinite loops in my code.
我一直试图解决的问题是处理代码中的无限循环。
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)
I've placed a bunch of print functions: print(lists)
and print(combolist)
. 我放置了一堆打印功能:
print(lists)
和print(combolist)
。 When I run it, lists and combolist are constantly updated and printed. 当我运行它时,列表和组合列表会不断更新和打印。 It then stops printing which is expected, but my program keeps running something.
然后它停止了预期的打印,但是我的程序继续运行。 It also never reaches
它也永远不会到达
combolist.remove("lol")
print(combolist)
I went through the effort of following the logic of my code to find any issues, but I didn't. 我努力按照代码的逻辑查找任何问题,但没有找到。 What's constantly looping in my code?
我的代码中不断循环的是什么?
comboloop = False
is creating a local variable that shadows your global called comboloop. comboloop = False
正在创建一个局部变量,该局部变量遮盖了全局变量comboloop。 If you add 如果添加
global comboloop
in your startingloop() function, the program exits 在startingloop()函数中,程序退出
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.