How to double each control variable in for loop in python

I want to implement a loop body like this in the cPP/ Java language:

for(auto i = 0; i < N; i += i) {
    //do something;
}

but I try to do this in python:

for i in range(0, N, i)
    -sharpdo something

the interpreter reminds me

undefined name "i"
Sep.27,2021

after thinking about it, write two first.

the initial I value of your cycle cannot be 0.

""" 1While """
i = 1
N = 50
while True:
    -sharp do something
    print("i = ", i)
    i += i
    if i >= N:
        break


""" 2 """
def gen(i, N):
    for index in range(i,N+1):
        -sharp 0,1,
        if index == i:
            yield index
            i += i
            
def gen2(i, N):
    while True:
        if i > N:
            break
        yield i
        i += i

-sharp gen  gen2 ,

-sharp i  N  1  10
for index in gen(1, 10):
    -sharp do something
    print(index)

-sharp i  N  3  50
for index in gen2(3, 50):
    -sharp do something
    print(index)

the first one uses the While True dead loop, in which the judgment logic jumps out of the loop.

the second one uses a generator, which is actually good to write this way. I prefer the second one, which looks better.

both ideas are the same, but they are written differently.

Let's talk about other implementations. I think it's enough.


for i in(2**e for e in range(N)):
    print(i)
    -sharp do something
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b3a837-2b998.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b3a837-2b998.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?