{"id":"more-loops-break","slug":"break","title":"Ending a loop with break","text":"You've used `break` since your first loops: it ends the loop at once, and the program goes on with the first line after the loop. Now that a loop can have a condition, many loops can be written either way. These two programs both count the guests typed in, until the user types `done`:\n\n```python\nguests = 0\nwhile True:\n    name = input(\"Guest: \")\n    if name == \"done\":\n        break\n    guests += 1\nprint(f\"{guests} guests\")\n```\n\n```input\nAda\nLinus\ndone\n```\n\n```output\nGuest: Ada\nGuest: Linus\nGuest: done\n2 guests\n```\n\n```python\nguests = 0\nname = \"\"\nwhile name != \"done\":\n    name = input(\"Guest: \")\n    if name != \"done\":\n        guests += 1\nprint(f\"{guests} guests\")\n```\n\nWith the same input, they print the same. But the second one needs a start value for `name` that's there only to get the loop going, and it checks for `done` twice: once in the condition and once more in the body, so that the word `done` isn't counted as a guest. With `break`, the check is in one place, right after the input. That's why a loop that reads input until some stop word is usually written with `while True` and `break`.\n\n### A condition and a break together\n\nA loop can have both: a condition for the usual way out, and a `break` for an early one. This bake sale goes on until 50 cookies are sold, unless the seller types 0 to close early:\n\n```python\nsold = 0\nwhile sold < 50:\n    cookies = int(input(\"Cookies sold (0 to close): \"))\n    if cookies == 0:\n        break\n    sold += cookies\nprint(f\"{sold} cookies sold\")\n```\n\n```input\n12\n20\n0\n```\n\n```output\nCookies sold (0 to close): 12\nCookies sold (0 to close): 20\nCookies sold (0 to close): 0\n32 cookies sold\n```\n\nHere the seller closed early. With the input 30 and 25, the loop would end by its condition instead: after the second round `sold` is 55, `55 < 50` is false, and the program prints `55 cookies sold` without asking again.\n\nEither way, the program goes on after the loop. If it needs to know which way the loop ended, it can check afterwards, for example with `if sold >= 50:`.\n","task":"Fill in the blank so the loop ends when the user types `stop`.\n\n```\nWord: hello\nhello!\nWord: stop\nStopped\n```\n","kind":"fill","seed":"while True:\n    word = input(\"Word: \")\n    if word == \"stop\":\n        [[break]]\n    print(word + \"!\")\nprint(\"Stopped\")\n","solution":"while True:\n    word = input(\"Word: \")\n    if word == \"stop\":\n        break\n    print(word + \"!\")\nprint(\"Stopped\")\n","hints":["When the word is `stop`, the loop should end at once, before the word is printed.","The command that ends a loop is one word, the same one you used in your first loops."],"tests":[{"name":"Test 1","stdin":"hello\nstop\n","expect":"Word: hello\nhello!\nWord: stop\nStopped","hidden":false},{"name":"Test 2","stdin":"a\nb\nc\nstop\n","expect":"Word: a\na!\nWord: b\nb!\nWord: c\nc!\nWord: stop\nStopped","hidden":true},{"name":"Test 3","stdin":"stop\n","expect":"Word: stop\nStopped","hidden":true}],"require":[],"forbid":[],"more":[{"task":"Write a guessing game. The secret number is 7 (write it into the program). The player has at most 4 guesses, each asked with `Guess: `. When a guess is right, the program prints `Correct!` and asks no more. If all 4 guesses are wrong, it prints `Out of guesses`. Use a loop whose condition counts the guesses, and `break` for the right guess.\n\n```\nGuess: 3\nGuess: 9\nGuess: 7\nCorrect!\n```\n","kind":"code","seed":"secret = 7\n","solution":"secret = 7\nguesses = 0\nwhile guesses < 4:\n    guess = int(input(\"Guess: \"))\n    guesses += 1\n    if guess == secret:\n        print(\"Correct!\")\n        break\nif guess != secret:\n    print(\"Out of guesses\")\n","hints":["Count the guesses in a helper variable that starts at 0. The loop goes on while it's below 4.","In the body, read a guess and add 1 to the count. If the guess equals `secret`, print `Correct!` and `break`.","After the loop, the last guess tells how the loop ended: if it isn't the secret, all 4 were wrong."],"tests":[{"name":"Test 1","stdin":"3\n9\n7\n","expect":"Guess: 3\nGuess: 9\nGuess: 7\nCorrect!","hidden":false},{"name":"Test 2","stdin":"7\n","expect":"Guess: 7\nCorrect!","hidden":true},{"name":"Test 3","stdin":"1\n2\n3\n4\n","expect":"Guess: 1\nGuess: 2\nGuess: 3\nGuess: 4\nOut of guesses","hidden":true},{"name":"Test 4","stdin":"1\n2\n3\n7\n","expect":"Guess: 1\nGuess: 2\nGuess: 3\nGuess: 7\nCorrect!","hidden":true}],"require":[{"pattern":"\\bbreak\\b","message":"End the loop with `break` when the guess is right."}],"forbid":[{"pattern":"while\\s+True","message":"Count the guesses in the loop's condition, such as `while guesses < 4:`, instead of `while True`."}]},{"task":"What does this program print? Type each line it prints.\n","kind":"predict","seed":"n = 0\nwhile n < 10:\n    n += 3\n    if n == 6:\n        break\n    print(n)\nprint(\"after\", n)\n","solution":"n = 0\nwhile n < 10:\n    n += 3\n    if n == 6:\n        break\n    print(n)\nprint(\"after\", n)\n","hints":["Go round by round: `n` becomes 3 in the first round. Is it 6?","In the second round `n` becomes 6, and `break` ends the loop before the `print` in the loop.","After the loop, `n` keeps the value it had when the loop ended."],"tests":[{"name":"Output","stdin":"","expect":"3\nafter 6","hidden":false}],"require":[],"forbid":[],"lines":["3","after 6"]}]}