How do I create a for-loop where the variable's value is equal to the stop value of range when the loop runs to the end in Python?

Viewed 3403

I'm having a conceptual problem porting this from C to Python:

int p;
for (p = 32; p < 64; p += 2) {
    if (some condition)
        break;
    do some stuff
}
return p;

Converting the loop to for p in range(32,64,2) does not work. This is because after the loop ends, p is equal 62 instead of 64.

I can do it with a while loop easily:

p = 32
while p < 64:
    if (some condition):
        break
    do some stuff
    p += 2
return p

But I'm looking for a Pythonic way.

3 Answers
Related