{"id":"conditionals-indentation","slug":"indentation","title":"Indentation and blocks","text":"Python decides which lines belong to an `if` by their **indentation**: the spaces at the start of each line. All the lines of a block must be indented by the same amount, and four spaces is the standard. You rarely count the spaces yourself: the editor on this site starts the line after a colon already indented, and the Tab key adds four spaces to a line.\n\nIndentation isn't just for looks. These two programs have the same lines, but the last line is indented differently:\n\n```python\nscore = 3\nprint(f\"Score: {score}\")\nif score > 5:\n    print(\"You win!\")\n    print(\"Game over.\")\n```\n\n```output\nScore: 3\n```\n\n```python\nscore = 3\nprint(f\"Score: {score}\")\nif score > 5:\n    print(\"You win!\")\nprint(\"Game over.\")\n```\n\n```output\nScore: 3\nGame over.\n```\n\nIn the first program, `print(\"Game over.\")` is part of the block, so it's skipped along with `print(\"You win!\")`. In the second, it's back at the left edge, after the block, so it runs every time.\n\n### Indentation errors\n\nWhen the indentation doesn't make sense, Python stops with an `IndentationError` before running anything. Here are the three you'll meet most often. A line after a colon that isn't indented leaves the `if` with an empty block:\n\n```python\ncode = input(\"Code: \")\nif code == \"1234\":\nprint(\"Access granted\")\n```\n\n```crash\n  File \"main.py\", line 3\n    print(\"Access granted\")\n    ^^^^^\nIndentationError: expected an indented block after 'if' statement on line 2\n```\n\nA line indented for no reason, with no colon before it:\n\n```python\nprint(\"Hello\")\n    print(\"World\")\n```\n\n```crash\n  File \"main.py\", line 2\n    print(\"World\")\nIndentationError: unexpected indent\n```\n\nAnd lines of the same block indented by different amounts:\n\n```python\nscore = 8\nif score > 5:\n    print(\"You win!\")\n  print(\"Well played.\")\n```\n\n```crash\n  File \"main.py\", line 4\n    print(\"Well played.\")\n                         ^\nIndentationError: unindent does not match any outer indentation level\n```\n\nLine 4 has two spaces: that's neither the block's four nor the left edge, so Python can't tell where the line belongs. Whatever the message, the fix is the same: look at the line it names and give it the indentation of the block it belongs to.\n","task":"The program stops with an `IndentationError`. Fix it, so that the right code grants access and `Goodbye` is printed every time. With the code `1234`, the run looks like this:\n\n```\nCode: 1234\nAccess granted\nGoodbye\n```\n","kind":"code","seed":"code = input(\"Code: \")\nif code == \"1234\":\nprint(\"Access granted\")\nprint(\"Goodbye\")\n","solution":"code = input(\"Code: \")\nif code == \"1234\":\n    print(\"Access granted\")\nprint(\"Goodbye\")\n","hints":["Read the error message. Which line does Python expect to be indented?","Indent `print(\"Access granted\")` by four spaces, so it belongs to the `if`. Leave `print(\"Goodbye\")` at the left edge, so it runs every time."],"tests":[{"name":"Test 1","stdin":"1234\n","expect":"Code: 1234\nAccess granted\nGoodbye","hidden":false},{"name":"Test 2","stdin":"0000\n","expect":"Code: 0000\nGoodbye","hidden":true}],"require":[],"forbid":[],"more":[{"task":"What does this program print? Type each line it prints.\n","kind":"predict","seed":"x = 3\nif x > 5:\n    print(\"A\")\n    print(\"B\")\nprint(\"C\")\nif x < 5:\n    print(\"D\")\nprint(\"E\")\n","solution":"x = 3\nif x > 5:\n    print(\"A\")\n    print(\"B\")\nprint(\"C\")\nif x < 5:\n    print(\"D\")\nprint(\"E\")\n","hints":["`x > 5` is false, so both indented lines under the first `if` are skipped.","The lines at the left edge run every time, and `x < 5` is true."],"tests":[{"name":"Output","stdin":"","expect":"C\nD\nE","hidden":false}],"require":[],"forbid":[],"lines":["C","D","E"]},{"task":"The restaurant's program offers free ice cream to everyone, but only kids under 13 should get it. Fix the indentation, so that the ice cream line belongs to the `if`. If the user types `8`, the run looks like this:\n\n```\nAge: 8\nKids' menu available.\nFree ice cream for kids!\nEnjoy your meal.\n```\n","kind":"code","seed":"age = int(input(\"Age: \"))\nif age < 13:\n    print(\"Kids' menu available.\")\nprint(\"Free ice cream for kids!\")\nprint(\"Enjoy your meal.\")\n","solution":"age = int(input(\"Age: \"))\nif age < 13:\n    print(\"Kids' menu available.\")\n    print(\"Free ice cream for kids!\")\nprint(\"Enjoy your meal.\")\n","hints":["Run it with an adult's age, such as 30. Which line shouldn't be there?","A line belongs to the `if` when it's indented like the other lines of its block: indent the ice cream line by four spaces."],"tests":[{"name":"Test 1","stdin":"8\n","expect":"Age: 8\nKids' menu available.\nFree ice cream for kids!\nEnjoy your meal.","hidden":false},{"name":"Test 2","stdin":"30\n","expect":"Age: 30\nEnjoy your meal.","hidden":true},{"name":"Test 3","stdin":"13\n","expect":"Age: 13\nEnjoy your meal.","hidden":true}],"require":[],"forbid":[]}]}