Search code examples
pythonfor-loopwhile-loopbreakcontinue

while for loop combination can't end up while loop


I'm developing a script to check if a data frame's column's sum is over three with a certain interval.

loc_number = 10 # start loc image number with 10
k = 2
while k < 3: 
    for i in range(0, int(len(target_df.index)/loc_number), loc_number):
        print(range(0, int(len(target_df.index)/loc_number), loc_number))
        print(loc_number, i)
        loc_target_df = target_df.loc[i: i+loc_number].sum(skipna=True, axis=0)
        print(loc_target_df)
        if any(x < 3 for x in loc_target_df):
            loc_number += 1
            print('heloo')
            break
        else:
            print('here')
            pass
    print('probably')
    continue

It seems at the beginning, it goes to 'if' and print out 'heloo' for a while, which means the sum of the certain column's values isn't over three. Ok. it's what I wanted.

However, when it goes to else, which means the cloumn's sum is over three, it should go to next 'for loop' not 'while'. but it goes to while loop again and again, so print out like this:

here
probablay
range(0, 14, 322)
322 0
10.000000     272
26.400000     210
31.680000     106
38.016000     113
45.619200     215
54.743040     146
65.691648     224
78.829978     255
94.595973     321
113.515168    308
136.218201    356
163.461842    270
196.154210    190
235.385052     97
282.462062     47
338.954475     32
406.745370      6
488.094443      3
dtype: int64
here
probablay
range(0, 14, 322)
322 0
10.000000     272
26.400000     210
31.680000     106
38.016000     113
45.619200     215
54.743040     146
65.691648     224
78.829978     255
94.595973     321
113.515168    308
136.218201    356
163.461842    270
196.154210    190
235.385052     97
282.462062     47
338.954475     32
406.745370      6
488.094443      3
dtype: int64
here
probablay
range(0, 14, 322)
322 0
10.000000     272
26.400000     210
31.680000     106
38.016000     113
45.619200     215
54.743040     146
65.691648     224
78.829978     255
94.595973     321
113.515168    308
136.218201    356
163.461842    270
196.154210    190
235.385052     97
282.462062     47
338.954475     32
406.745370      6
488.094443      3
dtype: int64
here
probablay
range(0, 14, 322)
322 0

what's the problem of my while for loop ?? and how to make the for loop work?


Solution

  • I will answer to the first question, maybe you could provide a bit more detail on what it should do once it passes the else then. I have the feeling you want the code to do something completely different than what it does right now.

    I hope it helps, although it is quite some text.

    Let's look at the execution:

    • you define your variables loc_number = 10, k = 2.
    • the code checks, if k < 3 which is true.
    • you define a range from 0 to int(len(target_df.index)/loc_number) = 450 in steps of loc_number = 10, which is an iterator which will set i = 0 in the first iteration
    • Now that i = 0, the code prints 10 0 and you calculate the sum of all elements of the dataframe from row 0 to row i + loc_number = 0 + 10 = 10. This returns a value for each column, which is again printed.
    • Now, you check if any of those values is smaller than 3 (x < 3), and if so, you increase loc_number by 1 (loc_number = 11), print 'heloo' and break out of the for loop.
    • The code prints 'probably'
    • As k < 3 == True, you initialize a new for loop with range(0, 409, 11) (ranging from 0 to 14 in steps of 11), which will again set i = 0 in the first iteration.
    • Now that i = 0, the code prints 11 0 and you calculate the sum of all elements of the dataframe from row 0 to row i + loc_number = 0 + 11 = 11. This returns a value for each column, which is again checked if any of these is smaller than 3.
    • As this is the case, loc_number += 1 = 12, it prints 'heloo' and you break out of the for loop again. The code prints 'probably'
    • This is repeated until loc_number = 322. Now, the range(0, 14, 322) only returns 0, so the for loop only sets i = 0, and you sum from row 0 to row 322, the code prints 322 0
    • Now, no value is smaller than 3, so the any(x < 3) evaluates to False and the else is invoced. There, loc_number stays the same and the code prints 'here'.
    • As the for loop does not find another element in the range() iterator, the for loop is ended and the code prints 'probably'.
    • You now initialize a for loop with unchanged range(0, 14, 322), which executes the same as before, no value is smaller than 3.
    • This circle never breaks, as the while statement is always true.

    Perhaps one point of confusion is, that the for in python behaves like foreach in other languages.

    Also, I'd suggest to run the following code snippet to better understand the range(start, stop, step) function, as I think this also behaves differently than you assumed:

    for i in range(0, 5, 2):
        print(i)
    
    for k in range(0, 5, 10):
        print(k)