TYPED CONVERSIONS
Use ordinary branches and supported loops, while ensuring every variable has a value before it is read.
Mode: typed conversion. Select Transpile → Typed, then click Transpile, or use the default CLI/API request. For runtime strings, shared containers, classes, and closures, see Runtime value conversions.
def classify(value: int) -> int:
result: int
if value < 0:
result = -1
elif value == 0:
result = 0
else:
result = 1
return result
classify(-5), classify(0), and classify(8) return -1, 0, and 1.
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.
def drain(count: int) -> int:
while count > 0:
count -= 1
else:
return 1
return 0
drain(3) and drain(0) both return 1: normal completion includes the zero-iteration case.
Use a condition that your code can eventually make false. PyCForge does not impose an execution-time limit on the generated program.
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
find_three(5) returns 3; find_three(3) and find_three(0) return 0 through loop else.
def sum_then_mark() -> int:
values = [1, 2, 3]
total = 0
for value in values:
total += value
else:
total += 1
return total
sum_then_mark() returns 7: the loop adds 1 + 2 + 3, then its else adds 1.
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.
else runs when a loop finishes normally, including when it runs zero times.break skips the else of the loop it exits.else.continue moves to the next iteration; it does not cancel eventual else execution. A function return skips it.else. Initialize a separate result before the loop, as in find_three above, and update that result inside it.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 in typed conversion. Runtime value conversion supports list, dictionary, and set comprehensions. Use a fixed local list or tuple when that suits the program.