{"id":"more-loops-nested","slug":"nested","title":"Loops inside loops","text":"The body of a loop can hold any statements, another loop included. A loop inside a loop is called a **nested loop**. The inner loop runs all its rounds during each single round of the outer loop.\n\nThis program asks for sizes until the user types 0. For each size, an inner loop builds a row of that many blocks, one block per round:\n\n```python\nwhile True:\n    size = int(input(\"Size: \"))\n    if size == 0:\n        break\n    row = \"\"\n    while size > 0:\n        row += \"[]\"\n        size -= 1\n    print(row)\n```\n\n```input\n3\n1\n0\n```\n\n```output\nSize: 3\n[][][]\nSize: 1\n[]\nSize: 0\n```\n\n(`\"[]\" * size` would build the same row. Here the inner loop shows how loops fit inside each other.)\n\nRead it from the outside in. The outer loop asks for a size. The inner loop, indented one more level, builds the row. When the inner loop is done, the rest of the outer loop's body prints the row, and the outer loop starts its next round.\n\n### How many times?\n\nTwo counters show how often each line runs:\n\n```python\nouter = 1\nwhile outer <= 3:\n    inner = 1\n    while inner <= 2:\n        print(f\"outer {outer}, inner {inner}\")\n        inner += 1\n    outer += 1\n```\n\n```output\nouter 1, inner 1\nouter 1, inner 2\nouter 2, inner 1\nouter 2, inner 2\nouter 3, inner 1\nouter 3, inner 2\n```\n\nThe inner loop runs completely, two rounds, in each of the three rounds of the outer loop: six lines in all. Note where `inner = 1` is: inside the outer loop, so the inner counter starts again from 1 every time. The next step looks at that closely.\n\n### break and continue in nested loops\n\n`break` and `continue` work on the innermost loop they're in. In the first example, the `break` is in the outer loop, so it ends the whole thing. A `break` in an inner loop would end only the inner loop: the outer loop would carry on with the rest of its body and its next round. In the same way, `continue` in an inner loop skips to the next round of the inner loop.\n","task":"What does this program print? Type each line it prints.\n","kind":"predict","seed":"number = 1\nwhile number <= 3:\n    other = 1\n    while other <= 3:\n        if other == number:\n            break\n        print(number, other)\n        other += 1\n    number += 1\n","solution":"number = 1\nwhile number <= 3:\n    other = 1\n    while other <= 3:\n        if other == number:\n            break\n        print(number, other)\n        other += 1\n    number += 1\n","hints":["The `break` is in the inner loop, so it ends only the inner loop. The outer loop goes on with its next number.","When `number` is 1, the inner loop breaks right away, since `other` is 1 too. Nothing is printed in that round.","When `number` is 2, `other` is 1 first (printed), then 2 (break)."],"tests":[{"name":"Output","stdin":"","expect":"2 1\n3 1\n3 2","hidden":false}],"require":[],"forbid":[],"lines":["2 1","3 1","3 2"],"more":[{"task":"Write a program that asks for a size and prints a grid of coordinates: one line per row, and on each line the pair `(row,column)` for every column, with spaces between the pairs. Rows and columns count from 1.\n\n```\nSize: 3\n(1,1) (1,2) (1,3)\n(2,1) (2,2) (2,3)\n(3,1) (3,2) (3,3)\n```\n","kind":"code","seed":"size = int(input(\"Size: \"))\n","solution":"size = int(input(\"Size: \"))\nrow = 1\nwhile row <= size:\n    line = \"\"\n    column = 1\n    while column <= size:\n        line += f\"({row},{column}) \"\n        column += 1\n    print(line)\n    row += 1\n","hints":["The outer loop goes through the rows, from 1 to the size. For each row, the inner loop goes through the columns.","Build each line in a string: start it empty in the outer loop, add `f\"({row},{column}) \"` in the inner loop, and print it after the inner loop.","A space at the very end of a line doesn't matter to the checks."],"tests":[{"name":"Test 1","stdin":"3\n","expect":"Size: 3\n(1,1) (1,2) (1,3)\n(2,1) (2,2) (2,3)\n(3,1) (3,2) (3,3)","hidden":false},{"name":"Test 2","stdin":"1\n","expect":"Size: 1\n(1,1)","hidden":true},{"name":"Test 3","stdin":"4\n","expect":"Size: 4\n(1,1) (1,2) (1,3) (1,4)\n(2,1) (2,2) (2,3) (2,4)\n(3,1) (3,2) (3,3) (3,4)\n(4,1) (4,2) (4,3) (4,4)","hidden":true}],"require":[],"forbid":[]},{"task":"Write a program that asks for words with the prompt `Word: ` until the user types `end`. For each word, it prints the word with every letter doubled.\n\n```\nWord: cat\nccaatt\nWord: no\nnnoo\nWord: end\n```\n","kind":"code","seed":"# Write your program here.\n","solution":"# Write your program here.\nwhile True:\n    word = input(\"Word: \")\n    if word == \"end\":\n        break\n    doubled = \"\"\n    index = 0\n    while index < len(word):\n        doubled += word[index] + word[index]\n        index += 1\n    print(doubled)\n","hints":["The outer loop reads the words and ends with `break` when the word is `end`.","For each word, an inner loop goes through its characters with an index, from 0 while it's below `len(word)`.","In the inner loop, add the character twice to a string that starts empty for every word: `doubled += word[index] + word[index]`."],"tests":[{"name":"Test 1","stdin":"cat\nno\nend\n","expect":"Word: cat\nccaatt\nWord: no\nnnoo\nWord: end","hidden":false},{"name":"Test 2","stdin":"end\n","expect":"Word: end","hidden":true},{"name":"Test 3","stdin":"a\nhello world\nend\n","expect":"Word: a\naa\nWord: hello world\nhheelllloo  wwoorrlldd\nWord: end","hidden":true}],"require":[],"forbid":[]}]}