{"id":"more-loops-continue","slug":"continue","title":"Skipping a round with continue","text":"`break` leaves the whole loop. `continue` leaves only the current round: the rest of the body is skipped, and Python goes straight back to the top of the loop, checks the condition, and starts the next round if it's true.\n\nThis program adds up prices, but skips any price over 100, which it takes to be a typo:\n\n```python\ntotal = 0\nwhile True:\n    price = int(input(\"Price (0 to stop): \"))\n    if price == 0:\n        break\n    if price > 100:\n        print(\"Skipped, too big\")\n        continue\n    total += price\nprint(f\"Total: {total}\")\n```\n\n```input\n20\n500\n15\n0\n```\n\n```output\nPrice (0 to stop): 20\nPrice (0 to stop): 500\nSkipped, too big\nPrice (0 to stop): 15\nPrice (0 to stop): 0\nTotal: 35\n```\n\nFor the price 500, `continue` skips `total += price`, and the next round asks for a new price.\n\n`continue` never does anything an `if` couldn't do: the same loop could put `total += price` in an `else`. But when a round can be skipped for several reasons, a check with `continue` for each one at the top of the body keeps the rest of the body simple, instead of nested deeper and deeper in `if`s.\n\n### continue and the update\n\nIn a loop that counts, `continue` holds a trap. This loop prints the numbers from 1 to 10, except the multiples of 3:\n\n```python\nnumber = 0\nwhile number < 10:\n    number += 1\n    if number % 3 == 0:\n        continue\n    print(number)\n```\n\n```output\n1\n2\n4\n5\n7\n8\n10\n```\n\nThe update `number += 1` comes first in the body, before the `continue`. Here is what happens with the update at the end, where it usually is:\n\n```python\nnumber = 1\nwhile number <= 10:\n    if number % 3 == 0:\n        continue\n    print(number)\n    number += 1\n```\n\nIt prints 1 and 2. Then, with `number` at 3, `continue` jumps back to the top before the update. `number` stays 3, every round is skipped the same way, and the loop never ends. So with `continue` in a loop, make sure the update happens on every path through the body.\n","task":"Fill in the blank so that empty lines are skipped and not counted as names.\n\n```\nName: Ada\nName:\nName: Linus\nName: end\n2 names\n```\n","kind":"fill","seed":"count = 0\nwhile True:\n    name = input(\"Name: \")\n    if name == \"end\":\n        break\n    if name == \"\":\n        [[continue]]\n    count += 1\nprint(f\"{count} names\")\n","solution":"count = 0\nwhile True:\n    name = input(\"Name: \")\n    if name == \"end\":\n        break\n    if name == \"\":\n        continue\n    count += 1\nprint(f\"{count} names\")\n","hints":["For an empty line, the rest of this round (the counting) should be skipped, and the loop should go on with the next name.","The command that skips to the next round of a loop is `continue`."],"tests":[{"name":"Test 1","stdin":"Ada\n\nLinus\nend\n","expect":"Name: Ada\nName:\nName: Linus\nName: end\n2 names","hidden":false},{"name":"Test 2","stdin":"\n\nend\n","expect":"Name:\nName:\nName: end\n0 names","hidden":true},{"name":"Test 3","stdin":"Grace\nTim\nGuido\nend\n","expect":"Name: Grace\nName: Tim\nName: Guido\nName: end\n3 names","hidden":true}],"require":[],"forbid":[],"more":[{"task":"Write a program that reads lines with the prompt `> ` until the user types `quit`. A line that starts with `#` is a comment: skip it. Print every other line back with `You said: ` in front of it. You can assume that no line is empty.\n\n```\n> hello\nYou said: hello\n> # just a note\n> bye\nYou said: bye\n> quit\n```\n","kind":"code","seed":"# Write your program here.\n","solution":"# Write your program here.\nwhile True:\n    line = input(\"> \")\n    if line == \"quit\":\n        break\n    if line[0] == \"#\":\n        continue\n    print(f\"You said: {line}\")\n","hints":["Read the lines in a `while True` loop, and `break` when the line is `quit`.","The first character of the line is `line[0]`. If it's `\"#\"`, use `continue` to skip the rest of the round.","The `print` comes last in the body, so it runs only for lines that weren't skipped."],"tests":[{"name":"Test 1","stdin":"hello\n# just a note\nbye\nquit\n","expect":"> hello\nYou said: hello\n> # just a note\n> bye\nYou said: bye\n> quit","hidden":false},{"name":"Test 2","stdin":"#a\n#b\nquit\n","expect":"> #a\n> #b\n> quit","hidden":true},{"name":"Test 3","stdin":"one # two\nquit\n","expect":"> one # two\nYou said: one # two\n> quit","hidden":true},{"name":"Test 4","stdin":"quit\n","expect":"> quit","hidden":true}],"require":[{"pattern":"\\bcontinue\\b","message":"Skip the comment lines with `continue`."}],"forbid":[]},{"task":"This program should print the numbers from 1 to 10, except the multiples of 4, but it gets stuck. Fix it, and keep the `continue`.\n\n```\n1\n2\n3\n5\n6\n7\n9\n10\n```\n","kind":"code","seed":"number = 1\nwhile number <= 10:\n    if number % 4 == 0:\n        continue\n    print(number)\n    number += 1\n","solution":"number = 0\nwhile number < 10:\n    number += 1\n    if number % 4 == 0:\n        continue\n    print(number)\n","hints":["What happens when `number` is 4? Follow the lines one by one, and look at what `continue` skips.","`continue` skips the update, so `number` stays 4 forever. The update has to happen before the `continue`.","Move `number += 1` to the start of the body. Then `number` must start at 0, and the condition becomes `number < 10`, so the last number is still 10."],"tests":[{"name":"Output","stdin":"","expect":"1\n2\n3\n5\n6\n7\n9\n10","hidden":false}],"require":[{"pattern":"\\bcontinue\\b","message":"Keep the `continue` and fix the order of the lines instead."}],"forbid":[]}]}