Skip to content
Menu
Learn at your paceChoose your scheduleMeet tutors online

Learning guides

Debug a short Python for loop

Learn to trace range(), compare actual output with expected output and test a small correction in Python.

Write the expected output

For a loop that should display 1 through 5, write the five numbers down first. Then run a loop using for i in range(5): print(i) and note the actual output: 0 through 4.

Trace each loop iteration

range(5) starts at zero and stops before five. To start at one and include five, use range(1, 6). Read the first and last values before running the revised loop.

Check the boundary cases

Test a one-item range such as range(1, 2) and an empty range such as range(1, 1). If you need help, share the shortest reproducible example, expected output and actual output without account credentials or API keys.

Cookie settings

Essential cookies support sign-in, account security and lesson functionality. They are required when you use those features.

Essential functionalityAlways active

Optional analytics and advertising controls are not configured in this interface. External services, such as a YouTube video you choose to play, may use their own technologies.

Read the cookie policy

Prefer the full sign-in page? ↗