{"id":"simple-loops-counting","slug":"counting","title":"Loops that count","text":"A loop doesn't have to read input. Often a variable changes a little on every iteration, and the loop stops when the variable reaches a limit:\n\n```python\nnumber = 1\nwhile True:\n    print(number)\n    number += 1\n    if number > 5:\n        break\nprint(\"Done!\")\n```\n\n```output\n1\n2\n3\n4\n5\nDone!\n```\n\nFollow it through: `number` starts at 1. Each iteration prints it and adds one to it. After printing 5, `number` becomes 6, the condition `number > 5` is true, and `break` ends the loop.\n\nThe variable can change in other ways too. Remember `-=` and `*=` from part 1: `seconds -= 3` is short for `seconds = seconds - 3`, and `n *= 2` doubles `n`. Here is a countdown in steps of 3:\n\n```python\nseconds = 10\nwhile True:\n    if seconds < 0:\n        break\n    print(seconds)\n    seconds -= 3\nprint(\"Liftoff!\")\n```\n\n```output\n10\n7\n4\n1\nLiftoff!\n```\n\nAfter 1 comes -2, which is below 0, so the loop stops before printing it.\n\n### Where the check goes\n\nIn the first loop the check comes after the `print`, in the second one before it. Where you put the `if` with the `break` decides which values get printed. Here the check comes right after the variable changes, but before the `print`:\n\n```python\nnumber = 1\nwhile True:\n    number += 1\n    if number > 5:\n        break\n    print(number)\n```\n\n```output\n2\n3\n4\n5\n```\n\nNow the first value printed is 2, because `number` grows before it's printed for the first time. When a loop prints one value too many or too few, follow the variable one iteration at a time, on paper or with a `print`, and you'll see where the check should go.\n\nOne more thing: the variable must change inside the loop. Leave out `number += 1`, and `number > 5` never becomes true, so the loop never ends.\n","task":"Fill in the blanks, so the program prints four laps and then ends the race:\n\n```\nLap 1\nLap 2\nLap 3\nLap 4\nRace over!\n```\n","kind":"fill","seed":"lap = 1\nwhile True:\n    print(f\"Lap {lap}\")\n    if lap == [[4]]:\n        break\n    lap [[+=]] 1\nprint(\"Race over!\")\n","solution":"lap = 1\nwhile True:\n    print(f\"Lap {lap}\")\n    if lap == 4:\n        break\n    lap += 1\nprint(\"Race over!\")\n","hints":["The loop should stop right after it prints the last lap.","The check compares `lap` with the number of the last lap, 4. After it, `lap += 1` moves on to the next lap."],"tests":[{"name":"Output","stdin":"","expect":"Lap 1\nLap 2\nLap 3\nLap 4\nRace over!","hidden":false}],"require":[],"forbid":[],"more":[{"task":"What does this program print? Type each line it prints.\n","kind":"predict","seed":"n = 1\nwhile True:\n    n *= 2\n    if n > 20:\n        break\n    print(n)\nprint(\"end\", n)\n","solution":"n = 1\nwhile True:\n    n *= 2\n    if n > 20:\n        break\n    print(n)\nprint(\"end\", n)\n","hints":["`n *= 2` doubles `n`, so it goes 2, 4, 8 and so on.","The check comes before the `print`, so a value over 20 is never printed inside the loop.","After the loop, `n` still holds the value that ended it."],"tests":[{"name":"Output","stdin":"","expect":"2\n4\n8\n16\nend 32","hidden":false}],"require":[],"forbid":[],"lines":["2","4","8","16","end 32"]},{"task":"Write a program that asks for a number and prints its multiplication table, from 1 to 10 times the number:\n\n```\nNumber: 7\n7 x 1 = 7\n7 x 2 = 14\n7 x 3 = 21\n7 x 4 = 28\n7 x 5 = 35\n7 x 6 = 42\n7 x 7 = 49\n7 x 8 = 56\n7 x 9 = 63\n7 x 10 = 70\n```\n","kind":"code","seed":"number = int(input(\"Number: \"))\n","solution":"number = int(input(\"Number: \"))\nfactor = 1\nwhile True:\n    print(f\"{number} x {factor} = {number * factor}\")\n    factor += 1\n    if factor > 10:\n        break\n","hints":["You need a second variable that counts from 1 to 10. Set it to 1 before the loop.","Each iteration prints one line, `f\"{number} x {factor} = {number * factor}\"`, and adds one to the counter.","Stop the loop with `break` once the counter has gone past 10."],"tests":[{"name":"Test 1","stdin":"7\n","expect":"Number: 7\n7 x 1 = 7\n7 x 2 = 14\n7 x 3 = 21\n7 x 4 = 28\n7 x 5 = 35\n7 x 6 = 42\n7 x 7 = 49\n7 x 8 = 56\n7 x 9 = 63\n7 x 10 = 70","hidden":false},{"name":"Test 2","stdin":"3\n","expect":"Number: 3\n3 x 1 = 3\n3 x 2 = 6\n3 x 3 = 9\n3 x 4 = 12\n3 x 5 = 15\n3 x 6 = 18\n3 x 7 = 21\n3 x 8 = 24\n3 x 9 = 27\n3 x 10 = 30","hidden":true},{"name":"Test 3","stdin":"12\n","expect":"Number: 12\n12 x 1 = 12\n12 x 2 = 24\n12 x 3 = 36\n12 x 4 = 48\n12 x 5 = 60\n12 x 6 = 72\n12 x 7 = 84\n12 x 8 = 96\n12 x 9 = 108\n12 x 10 = 120","hidden":true}],"require":[{"pattern":"\\bwhile\\s+True\\b","message":"Use a `while True` loop."},{"pattern":"\\bprint\\s*\\(","max":1,"message":"Print the lines with one `print` inside the loop."}],"forbid":[]}]}