Skip to content

Values Within A Round

Some values depend on others in the same row. Write them as ordinary assignments in the loop body. These temporaries last for one round, run in the order their inputs require, and appear in their own trace columns.

Here interest and amount are computed within a year; principal' carries the amount to the next year.

def compound(principal0: num, years: num) -> num:
init: year = 1; principal = principal0
while year <= years:
interest = principal * 0.1
amount = principal + interest
principal' = amount
year' = year + 1
return principal
print(compound(100, 3))
Run in playground
Output
133.1