Numeric for to range
Lesson 17 of 48 in Coddy's Python for Luau Developers course.
Luau numeric for includes its endpoint when the step reaches it, while Python range excludes its stop. Python for directly iterates element objects without requiring an ipairs-style pair. Reassigning the loop name does not replace an integer list slot, but mutating a nested object changes that shared object. Build a new list or assign indexed elements when replacement rather than observation is intended.
Luau example
for _, value in ipairs({2, 4}) do print(value) end
for i = 0, 3 do print(i) endPython
for value in [2, 4]:
print(value)
for i in range(3):
print(i)Porting trap
Python range(1, 3) visits one and two, unlike Luau for i = 1, 3, which also visits three.
Implement the specified API in the editable Python files. The locked driver.py runs the program and supplies input/output. Keep its imports and names unchanged.
Challenge
BeginnerRead two integer lines, start then stop, each between -100 and 100. Print the sum of all integers from start through stop, inclusive. If start > stop, print 0. Print one integer and a newline. Keep the supplied input scaffold.
Your code goes in main.py. The locked driver.py starts your program and cannot be edited.
Try it yourself
# Starts the program in main.py. Edit main.py to solve the challenge.
from runpy import run_module
run_module("main", run_name="__main__")
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Python for Luau Developers
1From Luau to Python
Run Your First Python ProgramPrinting and TextNames and Local ScopeNumbers and ConversionSuites Instead of endRecap: Rewrite a Receipt4Iteration and Transformations
Numeric for to rangepairs, ipairs, and itemsLoop Control and SkipsComprehensions and SortingRecap: Build a Ranking2Values and Decisions
nil Versus NoneTable Identity and EqualityTruthiness Is Differentelif and Value SelectionRecap: Eligibility Rules3Tables to Typed Collections
Sequence Tables to ListsTuples and Multiple ReturnsRecords to DictionariesSets and Shared TablesRecap: Clean an Inventory6Errors and Modules
Input and Exact Textpcall to Exceptionsrequire to importHints and Runtime TypesRecap: Validate a Batch9Project: Order Processing
Create the Order ModelAdd Pricing PoliciesPractice on your own: Online Python compiler