for loop - java and Python work differently

54 Views Asked by At

I want to modify variable y and it works in Java as below

for(int x=0;x<4;x++)
          {
            for(int y=0;y<3;y++)
            {
                System.out.print(y);
                if(y==1){y+=1;}              
            } 
          } // output == 01010101

But when I try to implement the same logic in Python it doesn't work as below

for x in range(0,4):
    for y in range(0,3):
        print(y, end='')
        if y==1:
            y+=1 # output == 012012012012

is there way to modify a variable in inner for-range loop in python?

1

There are 1 best solutions below

0
On

This is the code which is working, just change the range of y from (0, 3) to (0, 2):

for x in range (0, 4):
    for y in range (0, 2):
        print(y, end = '')
        if y == 1:
            y += 1