CONVERSION REFERENCE

Branches and loops

Use ordinary branches and supported loops, while ensuring every variable has a value before it is read.

If, elif, and else

Use a value assigned in a branch

def classify(value: int) -> int:
    result: int
    if value < 0:
        result = -1
    elif value == 0:
        result = 0
    else:
        result = 1
    return result

Every path reaching the final return assigns result, and every assignment uses the same type. Omitting the final else would leave some inputs without a value. Either cover every continuing branch or initialize the result before the conditional. Branches that return early do not need to assign a value used only by the remaining branches.

While loops

def drain(count: int) -> int:
    while count > 0:
        count -= 1
    else:
        return 1
    return 0

Use a condition that your code can eventually make false. PyCForge does not impose an execution-time limit on the generated program.

Range loops

Use range(stop), range(start, stop), or range(start, stop, step). Arguments must be integers; an explicit step must be a nonzero integer literal. Use a fresh loop-variable name and do not assign to it inside the loop.

def find_three(limit: int) -> int:
    result = -1
    for index in range(limit):
        if index == 3:
            result = index
            break
    else:
        result = 0
    return result

Fixed-container iteration

def sum_then_mark() -> int:
    values = [1, 2, 3]
    total = 0
    for value in values:
        total += value
    else:
        total += 1
    return total

Lists and tuples yield their elements in order. Dictionaries yield their fixed keys in insertion order. Set iteration is unsupported. See Containers and records for element and size limits.

When loop else runs

A loop may run zero times. Do not use its loop variable after the loop or in else. Initialize a separate result before the loop, as in find_three above, and update that result inside it.

If a loop is rejected

Check for a dynamic or zero range step, keyword arguments to range, multiple loop targets, reassignment of the loop variable, a variable used outside its supported scope, or an unsupported iterable. Generators and comprehensions are unsupported. Use a fixed local list or tuple when that suits the program.