So I just started out with smartpy and I am kinda confused about the point of having separate expression for loops. My understanding is that it is may be because it's a part of on-chain computations, but I'm not quite sure.

When using the default for loop expression, I got this error:

Error: 'Expr' object cannot be interpreted as an integer

 for i in range(1, params.op + 1):

1 Answer 1


Your understanding is quite correct.

SmartPy is a Python library, not a Python-inspired language so we use Python everywhere and overload Python's constructions. This approach is called meta-programming and is extremely important for bigger templates.

Control operators such as if, for, while cannot be overloaded in Python. So we have our own pure-python ones sp.if_, sp.for_, etc., and use some syntax transformation, called syntactic sugar, to present them nicely to developers.

Some information is here: https://www.smartpy.io/dev/reference.html#_control_and_syntactic_sugar

Your first error is in the range, you should use sp.range since params.op is a SmartPy expression, not an integer (as Python is telling you).

Then, you will need to you sp.for as SmartPy will tell you because this needs to be executed on-chain, not at compile time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.