Home > OS >  Why is the value of `i` not changing to what I'm changing it to inside the loop in python
Why is the value of `i` not changing to what I'm changing it to inside the loop in python

Time:10-23

I want to change the value of i in the loop to high once x = True but I don't know why every time the loop starts again, value of i changes to i not to high 1 here's the code :

    def max_min(self, data, high, low):
        list1 = []
        list2 = []
        x = False
        c = 0
        for i in range(c, len(data)):
            print(f"i = {i}")
            if i == low:
                if high > low:
                    list1.append(data[i])
                    list2 = data[low   1:high]
                    x = True #once this condition is true
                    print(f"list2 = {list2}")
                else:
                    list2 = data[low   1:]
                    print(f"list2 = {list2}")
            else:
                list1.append(data[i])
            print(f"list1 = {list1}")
        if x == True:
            c = high
        return list1, list2

FOR MORE CLARITY : HERE'S the output with every iteration, Assume high = 6,for i in range(7):

i = 0
i = 1
i = 2
i = 3
i = 4
i = 5
i = 6

whereas ideally it should be

i = 0
i = 1
i = 2
i = 3 
# x = True, high = 6
i = 6 #high = 6

CodePudding user response:

I would offer three suggestions:

  1. Change i rather than c to high.
  2. Change it inside the loop, not outside.
  3. Realise that it's not going to work anyway since the for in will revert it to the next item in sequence regardless of what you do with the loop control variable :-)

That last point you will see in action if you attempt to run:

for i in range(10):
    print(i)
    i = 9999

It will print 0..9 despite the changes to i inside the loop. If you want a loop which will allow your changes to hold, for in is not what you're looking for. Try a while loop instead, something like:

i = 0
while i < len(data):
    # Weave your magic here, including changing i to
    # whatever you want.

    i  = 1
# Carry on ...

CodePudding user response:

For you to be able to actively influence the loop, you need a while loop. If you manually set i at some point in the for loop, it will be automatically reset at the beginning of the next iteration to the next value in line. E.g.:

for i in range(10):
    if i == 3:
        i = 100
    print(i)

In a while loop however, you can change the iterating variable / condition:

i = 0
while i < 10:
    i  = 1
    if i == 3:
        i = 100
    print(i)

Translated to your case, I guess this is what you end up with:

def max_min(data, high, low):
    list1 = []
    list2 = []
    x = False
    i = 0
    while i < len(data):
        print(f"i = {i}")
        if i == low:
            if high > low:
                list1.append(data[i])
                list2 = data[low   1:high]
                x = True  # once this condition is true
                print(f"list2 = {list2}")
            else:
                list2 = data[low   1:]
                print(f"list2 = {list2}")
        else:
            list1.append(data[i])
        print(f"list1 = {list1}")

        if x is True:
            i = high
            x = False
        else:
            i  = 1

    return list1, list2


max_min([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 6, 3)

CodePudding user response:

for i in range(7): is i in [0,1,2,3,4,5,6] fixed. No matter how many times you change the i value within the loop,in the next iteration it'll follow the sequence [0,1,2,3,4,5,6].
Also, your if x == True: seems to be outside of for-loop by indentation.
Try while-loop

i = c
while i<len(data):
    ....
    if x == True:
         i = high
    else:
         i  = 1
  • Related