{"id":"simple-loops-while-true","slug":"while-true","title":"Repeating with while True","text":"So far every line of a program has run at most once. But programs often need to do the same thing many times: ask for one more number, play one more round. A **loop** repeats a block of code. The simplest loop in Python is `while True`, and the command `break` ends it:\n\n```python\nwhile True:\n    word = input(\"Say something (bye to quit): \")\n    if word == \"bye\":\n        break\n    print(f\"You said: {word}\")\nprint(\"Goodbye!\")\n```\n\n```input\nhello\nloops are fun\nbye\n```\n\n```output\nSay something (bye to quit): hello\nYou said: hello\nSay something (bye to quit): loops are fun\nYou said: loops are fun\nSay something (bye to quit): bye\nGoodbye!\n```\n\nHere is how it works:\n\n- `while True:` starts the loop, and the indented lines after it are the loop's block.\n- When Python gets to the end of the block, it jumps back to the `while` line and runs the block again. One run through the block is called an **iteration**.\n- `break` ends the loop at once. Python goes on from the first line after the loop's block, here the `print(\"Goodbye!\")`.\n\nThe `break` is inside an `if`, so the loop ends only when the user types `bye`. On that iteration the last line of the block never runs, because the loop has already ended.\n\n### A loop that never ends\n\nIf no `break` can ever run, the loop goes on forever. This is called an **infinite loop**. This one should stop when the user types 0, but it never does:\n\n```python\nwhile True:\n    answer = input(\"Play another round? (1 = yes, 0 = no) \")\n    if answer == 0:\n        break\n    print(\"Here we go again!\")\n```\n\n`input` returns a string, so when the user types 0, `answer` is the string `\"0\"`, not the number `0`, and `\"0\" == 0` is false. The condition can never be true, so whatever the user types, the loop asks again, forever. The fix is to compare with a string, `answer == \"0\"`, or to convert the answer with `int` first. Every `while True` loop needs a `break` that will run sooner or later. (A loop that doesn't wait for input and never reaches its `break` keeps running as fast as it can; on this site, a program that runs for more than a few seconds is stopped.)\n\n### Asking until the answer is acceptable\n\nA loop is also the way to ask again until the user types something the program can use:\n\n```python\nwhile True:\n    age = int(input(\"Your age: \"))\n    if age >= 0:\n        break\n    print(\"An age can't be negative. Try again.\")\nprint(f\"Thanks! You are {age}.\")\n```\n\n```input\n-5\n31\n```\n\n```output\nYour age: -5\nAn age can't be negative. Try again.\nYour age: 31\nThanks! You are 31.\n```\n\nNotice the order inside the loop: read the input, stop if it's fine, and only then print the complaint. The complaint is printed only when the loop didn't stop.\n","task":"Fill in the blanks, so the program keeps asking for commands until the user types `quit`:\n\n```\nCommand: jump\nRunning jump...\nCommand: run\nRunning run...\nCommand: quit\nShutting down.\n```\n","kind":"fill","seed":"[[while]] True:\n    command = input(\"Command: \")\n    if command == \"quit\":\n        [[break]]\n    print(f\"Running {command}...\")\nprint(\"Shutting down.\")\n","solution":"while True:\n    command = input(\"Command: \")\n    if command == \"quit\":\n        break\n    print(f\"Running {command}...\")\nprint(\"Shutting down.\")\n","hints":["The first line starts a loop that repeats until something ends it.","The loop starts with `while True:`, and the command that ends it is `break`."],"tests":[{"name":"Test 1","stdin":"jump\nrun\nquit\n","expect":"Command: jump\nRunning jump...\nCommand: run\nRunning run...\nCommand: quit\nShutting down.","hidden":false},{"name":"Test 2","stdin":"quit\n","expect":"Command: quit\nShutting down.","hidden":true},{"name":"Test 3","stdin":"fly\nquit\n","expect":"Command: fly\nRunning fly...\nCommand: quit\nShutting down.","hidden":true}],"require":[],"forbid":[],"more":[{"task":"Write a program for a café. It keeps asking what the customer would like and answers each order, until the customer types `nothing`. Then it says goodbye.\n\n```\nWhat would you like? tea\nOne tea coming up!\nWhat would you like? cake\nOne cake coming up!\nWhat would you like? nothing\nThank you, see you again!\n```\n","kind":"code","seed":"# Write your program below this line.\n","solution":"# Write your program below this line.\nwhile True:\n    order = input(\"What would you like? \")\n    if order == \"nothing\":\n        break\n    print(f\"One {order} coming up!\")\nprint(\"Thank you, see you again!\")\n","hints":["Start with `while True:` and read the order inside the loop, so each iteration asks again.","Check for `nothing` right after reading, and `break` if that's what was typed. Otherwise print the answer with an f-string.","The goodbye line comes after the loop, at the left edge."],"tests":[{"name":"Test 1","stdin":"tea\ncake\nnothing\n","expect":"What would you like? tea\nOne tea coming up!\nWhat would you like? cake\nOne cake coming up!\nWhat would you like? nothing\nThank you, see you again!","hidden":false},{"name":"Test 2","stdin":"nothing\n","expect":"What would you like? nothing\nThank you, see you again!","hidden":true},{"name":"Test 3","stdin":"coffee\nwater\nmuffin\nnothing\n","expect":"What would you like? coffee\nOne coffee coming up!\nWhat would you like? water\nOne water coming up!\nWhat would you like? muffin\nOne muffin coming up!\nWhat would you like? nothing\nThank you, see you again!","hidden":true}],"require":[{"pattern":"\\bwhile\\s+True\\b","message":"Use a `while True` loop."},{"pattern":"\\bbreak\\b","message":"End the loop with `break`."}],"forbid":[]},{"task":"Write a program that turns minutes into hours and minutes. It keeps asking for minutes until the user types 0, and then prints `Bye!`\n\n```\nMinutes (0 to quit): 135\n2 h 15 min\nMinutes (0 to quit): 59\n0 h 59 min\nMinutes (0 to quit): 0\nBye!\n```\n","kind":"code","seed":"# Write your program below this line.\n","solution":"# Write your program below this line.\nwhile True:\n    minutes = int(input(\"Minutes (0 to quit): \"))\n    if minutes == 0:\n        break\n    print(f\"{minutes // 60} h {minutes % 60} min\")\nprint(\"Bye!\")\n","hints":["Read the number inside a `while True` loop and `break` when it's 0.","The whole hours are `minutes // 60`, and the minutes left over are `minutes % 60`.","Print both in one f-string: `f\"{minutes // 60} h {minutes % 60} min\"`."],"tests":[{"name":"Test 1","stdin":"135\n59\n0\n","expect":"Minutes (0 to quit): 135\n2 h 15 min\nMinutes (0 to quit): 59\n0 h 59 min\nMinutes (0 to quit): 0\nBye!","hidden":false},{"name":"Test 2","stdin":"60\n0\n","expect":"Minutes (0 to quit): 60\n1 h 0 min\nMinutes (0 to quit): 0\nBye!","hidden":true},{"name":"Test 3","stdin":"1000\n7\n121\n0\n","expect":"Minutes (0 to quit): 1000\n16 h 40 min\nMinutes (0 to quit): 7\n0 h 7 min\nMinutes (0 to quit): 121\n2 h 1 min\nMinutes (0 to quit): 0\nBye!","hidden":true}],"require":[{"pattern":"\\bwhile\\s+True\\b","message":"Use a `while True` loop."}],"forbid":[]}]}