{"base":{"id":"cs9-06","subject":"Programming","title":"Count-controlled loops in Python","scene":"py9","steps":[{"mode":"range","eyebrow":"for and range","title":"Start, stop, step","body":"for i in range(start, stop, step) repeats a fixed number of times. The loop stops before it reaches the stop value. Change the three numbers and run it.","controls":[{"id":"a","label":"Start","min":0,"max":5,"step":1,"value":1},{"id":"b","label":"Stop","min":2,"max":12,"step":1,"value":6},{"id":"c","label":"Step","min":1,"max":3,"step":1,"value":1}],"actions":[{"id":"step","label":"Step"},{"id":"run","label":"Run"},{"id":"reset","label":"Reset"}]},{"mode":"turns","eyebrow":"Using a loop","title":"Five turns, one block of code","body":"A game gives each player a set number of turns. Instead of writing the same lines again and again, a loop repeats them. Change the number of turns.","controls":[{"id":"t","label":"Turns","min":1,"max":8,"step":1,"value":5}],"actions":[{"id":"step","label":"Step"},{"id":"run","label":"Run"},{"id":"reset","label":"Reset"}]},{"mode":"boundary","eyebrow":"Test the boundaries","title":"First and last pass","body":"We want the numbers 1 to 5. Try three loops and check the first and the last number printed. Which one is right?","actions":[{"id":"r0","label":"range(5)"},{"id":"r1","label":"range(1, 5)"},{"id":"r2","label":"range(1, 6)"},{"id":"run","label":"Run"}]},{"mode":"quiz","eyebrow":"Check yourself","title":"Count the passes","body":"for i in range(2, 8):\n    print(i)","question":"How many numbers are printed?","options":["8","7","6"],"answer":2,"right":"Correct. 2, 3, 4, 5, 6, 7: the stop value 8 is not included.","wrong":"Not quite. range stops before the stop value.","look":"range"}],"more":[{"type":"cards","id":"ideas","eyebrow":"Popular idea vs science","title":"Common mix-ups","items":[{"tag":"Popular idea","title":"\"range(1, 10) includes 10.\"","body":"It stops before 10, giving 1 to 9."},{"tag":"Popular idea","title":"\"range(5) starts at 1.\"","body":"It starts at 0: 0, 1, 2, 3, 4."},{"tag":"Popular idea","title":"\"Indentation in Python is just for looks.\"","body":"Indentation tells Python which lines are inside the loop."}]},{"type":"cards","id":"code","eyebrow":"Python","title":"Loop patterns","items":[{"tag":"Count","title":"1 to 10","body":"for i in range(1, 11):\n    print(i)","code":true},{"tag":"Step","title":"Even numbers","body":"for i in range(2, 11, 2):\n    print(i)   # 2 4 6 8 10","code":true},{"tag":"Repeat","title":"Five times","body":"for _ in range(5):\n    print(\"Hello\")","code":true}]},{"type":"activities","id":"try","eyebrow":"Try it at home","title":"Times table","items":[{"title":"Write a loop","time":"20 minutes","need":["A computer with Python, or paper"],"steps":["Write a loop that prints the 7 times table from 7 × 1 to 7 × 12.","Predict the first and last lines before running.","Change it to print only even answers.","Test: does it print exactly 12 lines?"],"why":"Checking the first and last pass catches off-by-one errors.","safety":"No safety risks."}]},{"type":"quiz","id":"quiz","eyebrow":"Quiz","title":"Six more questions","questions":[{"question":"What does range(4) produce?","options":["0, 1, 2, 3","1, 2, 3, 4","0, 1, 2, 3, 4"],"answer":0,"right":"Correct. It starts at 0 and stops before 4.","wrong":"Not quite. Start at 0."},{"question":"range(1, 20, 5) gives…","options":["1, 5, 10, 15, 20","1, 6, 11, 16","5, 10, 15"],"answer":1,"right":"Correct. Start 1, add 5, stop before 20.","wrong":"Not quite. Add the step each time."},{"question":"How do you print 1 to 100?","options":["range(1, 100)","range(1, 101)","range(100)"],"answer":1,"right":"Correct. Stop one beyond the last value.","wrong":"Not quite. The stop value is excluded."},{"question":"Which line is inside the loop?","options":["The line after the loop","The first line","The indented line"],"answer":2,"right":"Correct. Indentation shows it.","wrong":"Not quite. How does Python know?"},{"question":"for i in range(3): print(\"Hi\") prints Hi…","options":["3 times","2 times","4 times"],"answer":0,"right":"Correct. i = 0, 1, 2.","wrong":"Not quite. Count the values."},{"question":"Why use a loop instead of copying a line 5 times?","options":["Shorter and easier to change","It's slower","Python requires it"],"answer":0,"right":"Correct. Change one number to change the count.","wrong":"Not quite. Think of changing it later."}]},{"type":"checklist","id":"check","eyebrow":"Self-check","title":"I can explain…","items":["how for and range repeat code","why range stops before the stop value","how to choose start, stop and step","how to test the first and last pass"]},{"type":"keywords","id":"words","eyebrow":"Key words","title":"Words to know","items":[{"word":"for loop","meaning":"A loop that repeats a set number of times."},{"word":"range","meaning":"A Python function that makes a sequence of numbers."},{"word":"count-controlled","meaning":"Repeating a known number of times."},{"word":"indentation","meaning":"Spaces at the start of a line that show structure."},{"word":"boundary","meaning":"The first or last value of a range."}]},{"type":"sources","id":"sources","title":"Sources","items":["BBC Bitesize KS3 Computer Science","Raspberry Pi Foundation, Computing curriculum resources"]}]},"overlay":null}