简体   繁体   中英

Why won't this go back to the beginning of the while loop?

I am trying to create a roster and as part of the creation process the code iterates through a number of loops, creates several rosters, compares them to each other for "idealness" and then spits out the "most ideal/least worst" at the end.

The section below is the bit where I am trying to check if on a given day someone has been allocated a shift too close to the date the code is "working on", and if so, they should be removed from the list of available individuals, and the code should go back to the start of the while loop.

I have tried moving the continue to various points, created a large number of infinite loops (sigh), but I can't seem to get the process of selecting from the shortened list in whocando[r][3] to happen. If I was in Ruby I would use a redo , but I can't get this to work.

solution_found = False

whocando = [
    [date(2018, 1, 2), 'Tuesday', 1,
        ['XX', 'BB', 'UU', 'MM', 'GG'], ['PP', 'VV']],
    [date(2018, 1, 3), 'Wednesday', 1,
        ['UU', 'SS', 'NN', 'BB', 'GG'], ['HH', 'AA', 'FF', 'KK', 'QQ', 'TT']],
    [date(2018, 1, 4), 'Thursday', 1,
        ['AA', 'RR', 'MM', 'KK', 'BB', 'UU', 'QQ'], ['CC', 'HH', 'NN', 'XX', 'JJ']]
]

r = random.randrange(0,len(whocando))
while whocando[r][3] and not solution_found:
    chosenone = random.choice(whocando[r][3])

    # next bit iterates over a list of shifts already filled,
    # creates a list of dates and calls a function that compares the dates
    # with the current date to see if the two shifts are too close together

    checker = [k for k,v in interim_rota.items() if v == chosenone]
        for shift in checker:
            q = daysprox(working_date,shift) 

            # daysprox() returns a tuple of (True, proxtype), where proxtype
            # is an integer, if shifts are too close together

            if q[0]:
                this_iteration.too_close_by_func += 1
                try:
                    whocando[r][3].remove(chosenone)
                    continue
                    
                    # now I expect the while loop to start again and find a
                    # new choseone, but it doesn't, it proceeds into the next
                    # section of the code and adds the current choseone to
                    # the rota

                except ValueError:
                    traceback_output = traceback.format_exc()
                    print(traceback_output, whocando[r][3], chosenone)
            else:
                solution_found = True

Answer: It transpired that there was some nesting of loops outside this code....

I'm not sure if I understood 100%, but if you want to exit the for loop, you might want to use break instead of continue . For example:

>>> i = 0
>>> while i < 2:
...     print(i)
...     for s in "test":
...         if s == "s":
...             break
...         print(s)
...     i += 1
...
0
t
e
1
t
e

Not a fan of doing this, maybe a while loop with a flag might be easier and more understandable in flow. But this is just a personal opinion:)

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