{"id":"loop-conditions-while-condition","slug":"while-condition","title":"A loop with a condition","text":"In the last module, every loop started with `while True:`. Since `True` is always true, a loop like that would run forever on its own, so a `break` inside it did the stopping. But `while` can do more: like `if`, it takes any **condition**, and the loop runs as long as the condition is true.\n\nHere is a loop in the style of the last module. It counts to three:\n\n```python\nnumber = 1\nwhile True:\n    if number > 3:\n        break\n    print(number)\n    number += 1\nprint(\"Done\")\n```\n\n```output\n1\n2\n3\nDone\n```\n\nAnd here is the same loop with a condition:\n\n```python\nnumber = 1\nwhile number <= 3:\n    print(number)\n    number += 1\nprint(\"Done\")\n```\n\n```output\n1\n2\n3\nDone\n```\n\nThis is what Python does with it:\n\n1. It checks the condition `number <= 3`. If it's true, it runs the indented lines under the `while`, the **body** of the loop.\n2. At the end of the body, it goes back up to the `while` line and checks the condition again.\n3. As soon as the condition is false, it skips the body and goes on with the first line after the loop.\n\nOne pass through the body is an iteration of the loop, as in the last module. These lessons also call it a **round**, which is shorter. Here the body runs three times, with `number` at 1, 2 and 3. After the third round `number` is 4, the condition `4 <= 3` is false, and the loop ends.\n\nCompare the two conditions. The `if` before `break` says when to **stop**: `number > 3`. The `while` condition says when to **keep going**: `number <= 3`. One is the opposite of the other. The version with the condition is shorter, and a reader sees right at the top how long the loop runs.\n\n### The condition comes first\n\nPython checks the loop condition before every round, the first one included. If the condition is false from the start, the body doesn't run at all:\n\n```python\nlap = int(input(\"Starting lap: \"))\nwhile lap <= 5:\n    print(f\"Lap {lap}\")\n    lap += 1\nprint(\"Race over\")\n```\n\n```input\n3\n```\n\n```output\nStarting lap: 3\nLap 3\nLap 4\nLap 5\nRace over\n```\n\nWith 8 as the input, `8 <= 5` is false straight away, so after the prompt the program prints only `Race over`.\n\nThe condition is checked only there, at the top. If it becomes false in the middle of the body, the rest of the body still runs, and the loop ends when Python comes back up to check.\n","task":"Fill in the condition so the program prints the multiples of 5 from 5 to 50, each on its own line.\n","kind":"fill","seed":"number = 5\nwhile [[number <= 50‖number < 51‖number < 55]]:\n    print(number)\n    number += 5\n","solution":"number = 5\nwhile number <= 50:\n    print(number)\n    number += 5\n","hints":["The loop should keep going as long as `number` is at most 50.","\"At most\" is `<=`. Compare `number` with 50."],"tests":[{"name":"Output","stdin":"","expect":"5\n10\n15\n20\n25\n30\n35\n40\n45\n50","hidden":false}],"require":[],"forbid":[],"more":[{"task":"What does this program print? Type each line it prints.\n","kind":"predict","seed":"x = 20\nwhile x > 1:\n    print(x)\n    x = x // 2\nprint(\"Last:\", x)\n","solution":"x = 20\nwhile x > 1:\n    print(x)\n    x = x // 2\nprint(\"Last:\", x)\n","hints":["`//` divides and drops the remainder, so `5 // 2` is 2.","Before each round, check whether `x > 1` is still true. Write down `x` after every round.","The values of `x` are 20, 10, 5, 2 and then 1. Which of them get printed inside the loop?"],"tests":[{"name":"Output","stdin":"","expect":"20\n10\n5\n2\nLast: 1","hidden":false}],"require":[],"forbid":[],"lines":["20","10","5","2","Last: 1"]},{"task":"Write a program that asks `How many steps? ` and then prints one line per step, from `Step 1` up to the number typed in. After the steps it prints `At the top!`. Use a `while` loop with a condition, not `while True`.\n\n```\nHow many steps? 3\nStep 1\nStep 2\nStep 3\nAt the top!\n```\n","kind":"code","seed":"steps = int(input(\"How many steps? \"))\n","solution":"steps = int(input(\"How many steps? \"))\nstep = 1\nwhile step <= steps:\n    print(f\"Step {step}\")\n    step += 1\nprint(\"At the top!\")\n","hints":["You need a second variable that counts the steps. Give it the value 1 before the loop.","The loop goes on while the counter is at most `steps`: `while step <= steps:`.","In the body, print the line with an f-string, then add 1 to the counter. `At the top!` goes after the loop, without indentation."],"tests":[{"name":"Test 1","stdin":"3\n","expect":"How many steps? 3\nStep 1\nStep 2\nStep 3\nAt the top!","hidden":false},{"name":"Test 2","stdin":"1\n","expect":"How many steps? 1\nStep 1\nAt the top!","hidden":true},{"name":"Test 3","stdin":"0\n","expect":"How many steps? 0\nAt the top!","hidden":true},{"name":"Test 4","stdin":"6\n","expect":"How many steps? 6\nStep 1\nStep 2\nStep 3\nStep 4\nStep 5\nStep 6\nAt the top!","hidden":true}],"require":[],"forbid":[{"pattern":"while\\s+True","message":"Write the loop with a condition, such as `while step <= steps:`, instead of `while True`."}]}]}