Final Variable Until Next Iteration Python?

Viewed 22

I am coding a for loop in which I have a preassigned boolean named flag. In one particular case during this loop I need to keep flag as True (final) despite if I try to change it later to False. After I complete an iteration of this loop and return to the top, I would like my flag variable to become "changeable" once again. I was originally trying to use continue but it seemed inefficient. Are there any better ways to achieve this?

import random

border = []

numline = []

size = 100

for i in range (size):
    
    numline.append(round(i + 1 * random.random(), 2))
    
flag = True

total = 0

previous = total

avDif = (numline[size - 1] - numline[0] / (size - 1))

for i in range (1, size):
    
    total += numline[i] - numline[i - 1]
    
    if total >= avDif:
        
        if avDif - previous >= total - avDif or flag:
            
            border.append(numline[i])
            
            total = 0
            
            previous = total
            
            continue
        
        else:
            
            total = numline[i] - numline[i - 1]
            
            border.append(numline[i - 1])
            
    previous = total
    
    flag = False
    
0 Answers
Related