{"id":"terminology-statements-blocks","slug":"statements-and-blocks","title":"Statements and blocks","text":"In part 1 you wrote programs without worrying much about what their parts are called. Programmers use a handful of words for those parts all the time, and knowing them makes error messages, documentation and the rest of this course easier to follow. This module goes through the most important ones.\n\n### Statements\n\nA **statement** is one complete instruction in a program, something Python carries out. Every line of your programs in part 1 was a statement: `name = input(\"Your name: \")` asks a question and keeps the answer in a variable, and `print(f\"Hi, {name}!\")` greets the user by that name. Python carries out the statements one at a time, from the top down. Most statements fit on one line.\n\nA statement can also contain other statements. An `if` statement is a single statement made of its first line, with the condition, and the lines that belong to it:\n\n```python\ntemperature = 28\n\nif temperature > 25:\n    print(\"It's a warm day.\")\n    print(\"Remember to drink water.\")\n\nprint(\"See you later!\")\n```\n\n```output\nIt's a warm day.\nRemember to drink water.\nSee you later!\n```\n\nThis program has three statements at the top level: an assignment, an `if` statement and a `print`. The `if` statement has two more statements inside it.\n\n### Blocks\n\nYou have used blocks since the `if` statement in part 1. A **block** is a run of statements that Python handles as one unit, and in Python you mark it with indentation: all the lines of a block are indented by the same amount. Above, the two indented `print` lines make up the block of the `if`. When the condition is true Python runs the whole block, and when it's false Python skips the whole block. Where the indentation stops, the block stops too, which is why the last `print` runs every time. Which block a statement belongs to is decided by its indentation alone, so moving a line four spaces to the left or right can change what the program does.\n\nEverything that isn't indented belongs to the program's **main block**, the outermost one. Its lines have no indentation at all, so even one space before a line of the main block is an error:\n\n```python\nprint(\"Line one\")\n print(\"Line two\")\n```\n\n```crash\n  File \"main.py\", line 2\n    print(\"Line two\")\nIndentationError: unexpected indent\n```\n\nAn `IndentationError` is a kind of syntax error, so Python runs nothing at all, not even line 1. The same goes for a block that is missing: after a line that ends with a colon, Python expects at least one indented line, and stops with `IndentationError: expected an indented block` if there isn't one.\n","task":"What does this program print? Type each line it prints.\n","kind":"predict","seed":"level = 3\nif level > 5:\n    print(\"Expert\")\n    print(\"Hard mode on\")\nprint(\"Level\", level)\nif level > 1:\n    print(\"Past the tutorial\")\nprint(\"Go!\")\n","solution":"level = 3\nif level > 5:\n    print(\"Expert\")\n    print(\"Hard mode on\")\nprint(\"Level\", level)\nif level > 1:\n    print(\"Past the tutorial\")\nprint(\"Go!\")\n","hints":["The first condition is false (3 isn't greater than 5), so Python skips the whole block of that `if`, both lines.","`print(\"Level\", level)` starts at the left edge, so it isn't in any block and runs every time.","The second condition is true, so its block runs. The last line is in the main block again."],"tests":[{"name":"Output","stdin":"","expect":"Level 3\nPast the tutorial\nGo!","hidden":false}],"require":[],"forbid":[],"lines":["Level 3","Past the tutorial","Go!"],"more":[{"task":"Every player should see how many coins they have, and only players with at least 100 coins should be told to visit the shop. The thank-you line is for everyone. Two lines are in the wrong block. Fix the program, so that with 150 coins it prints:\n\n```\nHow many coins do you have? 150\nYou have 150 coins.\nYou can buy the sword.\nVisit the shop!\nThanks for playing.\n```\n","kind":"code","seed":"coins = int(input(\"How many coins do you have? \"))\nif coins >= 100:\n    print(f\"You have {coins} coins.\")\n    print(\"You can buy the sword.\")\nprint(\"Visit the shop!\")\nprint(\"Thanks for playing.\")\n","solution":"coins = int(input(\"How many coins do you have? \"))\nprint(f\"You have {coins} coins.\")\nif coins >= 100:\n    print(\"You can buy the sword.\")\n    print(\"Visit the shop!\")\nprint(\"Thanks for playing.\")\n","hints":["Try it with 40 coins: the player isn't told how many coins they have, but is still sent to the shop.","The line about the coins should run every time, so it belongs to the main block. Move it above the `if` line, at the left edge.","`Visit the shop!` belongs to the block of the `if`: indent it by four spaces, like the line above it."],"tests":[{"name":"Test 1","stdin":"150\n","expect":"How many coins do you have? 150\nYou have 150 coins.\nYou can buy the sword.\nVisit the shop!\nThanks for playing.","hidden":false},{"name":"Test 2","stdin":"40\n","expect":"How many coins do you have? 40\nYou have 40 coins.\nThanks for playing.","hidden":true},{"name":"Test 3","stdin":"100\n","expect":"How many coins do you have? 100\nYou have 100 coins.\nYou can buy the sword.\nVisit the shop!\nThanks for playing.","hidden":true}],"require":[],"forbid":[]},{"task":"A library charges 2 euros for every day a book is late. Write a program that asks how many days late the book is. If it's late (more than 0 days), the program prints two lines: `The book is late.` and the fee. It always ends with `Thank you for returning the book.`\n\n```\nHow many days late is the book? 4\nThe book is late.\nLate fee: 8 euros\nThank you for returning the book.\n```\n","kind":"code","seed":"# Write your program below this line.\n","solution":"# Write your program below this line.\ndays = int(input(\"How many days late is the book? \"))\nif days > 0:\n    print(\"The book is late.\")\n    print(f\"Late fee: {days * 2} euros\")\nprint(\"Thank you for returning the book.\")\n","hints":["Read the number with `int(input(...))`, so you can compare it and calculate with it.","The two lines about the fee form the block of an `if days > 0:` statement, indented by four spaces.","The fee is `days * 2`; an f-string puts it into the line: `f\"Late fee: {days * 2} euros\"`. The thank-you line is not indented."],"tests":[{"name":"Test 1","stdin":"4\n","expect":"How many days late is the book? 4\nThe book is late.\nLate fee: 8 euros\nThank you for returning the book.","hidden":false},{"name":"Test 2","stdin":"0\n","expect":"How many days late is the book? 0\nThank you for returning the book.","hidden":true},{"name":"Test 3","stdin":"11\n","expect":"How many days late is the book? 11\nThe book is late.\nLate fee: 22 euros\nThank you for returning the book.","hidden":true}],"require":[{"pattern":"\\bif\\b","message":"Use an `if` statement to decide whether the book is late."}],"forbid":[]}]}