Java to Python: Incrementation within "if" statement

Hi,

I was just wondering how you would convert that snippet to Python ?

for(int i=0; i<space.length; i++){
      int k=0;
      while(numCubes<space[i]){
        ...
        if((k++)>100){

          done = 1;

I’m not sure how to convert the if((k++)>100 part.

for i in range(len(space)):
    k = 0
    while numCubes < space[i];
        k += 1
        ...
        if k > 100:
            done = 1

Does that make sense ?

  • That’s the postfix version of the unary increment operator ++.
  • Like its counterpart prefix version, it also increments the value stored in a variable by 1.
  • However, the postfix version still evaluates as the original previous value instead!
  • As a workaround for Python, you can increment variable k after the if () {} block:
if k > 100:
    # ...

k += 1
1 Like

Sweet !

PS: Happy Birthday to you GoToLoop. Thank you for your valuable and continual help. Have a great day.

Oh, thx! :partying_face:

1 Like