Skip to content

BUG: Scope of index variable in for loop #176

@oyamad

Description

@oyamad

Bug found by @tetrapod54:

In this block, the variable i in for i in range(n) can be referred from outside of the for loop:

    while i < max_iter and error > tol:

        for i in range(n):
            v_next[i] = np.max(mcm.state_action_values(i, v))

        error = np.max(np.abs(v_next - v))
        i += 1  # <- This is always equal to n

        v[:] = v_next

Try this:

for i in range(5):
    pass

print(i)

You will get 4.

The index variable for the for loop (or that for the while loop) has to be something other than i.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions