简体   繁体   中英

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) . 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. If you add

global comboloop

in your startingloop() function, the program exits

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM