[Python] What the differences between these 3 for loops?

Discussão em 'Python' iniciado por Stack, Outubro 5, 2024.

  1. Stack

    Stack Membro Participativo

    def generate_coor(dimension_x,dimension_y):
    x,y=int(dimension_x/2),int(dimension_y/2)
    for i in range(0, x + 1):
    print(x)
    x=x+1

    generate_coor(100,27)


    The above code does terminate.

    def generate_coor(dimension_x,dimension_y):
    x,y=int(dimension_x/2),int(dimension_y/2)
    for i in range(0, x + 1):
    for j in range(0, y + 1):
    print(y)
    y=y+1

    generate_coor(100,27)


    The above code does not terminate

    def generate_coor(dimension_x,dimension_y):
    x,y=int(dimension_x/2),int(dimension_y/2)
    for i in range(0, y):
    for j in range(0, y + 1):
    print(y)
    y=y+1


    The above code does terminate

    What the differences between these 3 examples? According to the code, each iteration for x or y should be increase by 1 hence the loop should not terminate but some terminate, some does not?

    Continue reading...

Compartilhe esta Página