{"id":"conditionals-if","slug":"if-statement","title":"The if statement","text":"The programs you've written so far follow one straight path: Python runs every line, top to bottom, whatever the user types. Real programs choose: they warn when it's hot, congratulate the winner, ask a new user to pick a password. A **conditional statement** runs a group of lines only when a **condition** is true.\n\n```python\ntemperature = int(input(\"Temperature: \"))\nif temperature > 25:\n    print(\"It's hot today.\")\n    print(\"Remember to drink water.\")\nprint(\"Have a nice day!\")\n```\n\n```input\n30\n```\n\n```output\nTemperature: 30\nIt's hot today.\nRemember to drink water.\nHave a nice day!\n```\n\nIf the user types 18 instead, the condition `temperature > 25` is false. Python skips the two indented lines, and the whole run is just this:\n\n```text\nTemperature: 18\nHave a nice day!\n```\n\n### The parts of an if statement\n\n- The keyword `if` starts the statement.\n- `temperature > 25` is the condition. `>` means \"greater than\"; the next step shows the other ways to compare.\n- The line ends with a colon `:`.\n- The lines below it that are **indented**, moved to the right by four spaces, form a **block**. They run only when the condition is true.\n- The first line back at the left edge, `print(\"Have a nice day!\")`, is no longer part of the block. It runs every time, whatever the condition was.\n\nThe input decides which lines run, so the same program can behave differently each time. That's why the checks on this site run your program with several inputs: each one should take the right path.\n\n### Don't forget the colon\n\nWithout the colon at the end of the `if` line, Python can't read the program:\n\n```python\ntemperature = 30\nif temperature > 25\n    print(\"It's hot today.\")\n```\n\n```crash\n  File \"main.py\", line 2\n    if temperature > 25\n                       ^\nSyntaxError: expected ':'\n```\n\nThe `^` points to the end of line 2, where Python expected the colon. As with every syntax error, nothing runs at all.\n","task":"Fill in the blank so the program praises big orders. If the user types `150`, the run looks like this:\n\n```\nHow many items? 150\nThat's a big order!\nThank you!\n```\n","kind":"fill","seed":"items = int(input(\"How many items? \"))\nif items > 100[[:]]\n    print(\"That's a big order!\")\nprint(\"Thank you!\")\n","solution":"items = int(input(\"How many items? \"))\nif items > 100:\n    print(\"That's a big order!\")\nprint(\"Thank you!\")\n","hints":["Something is missing at the end of the `if` line.","Every `if` line ends with a colon: `:`."],"tests":[{"name":"Test 1","stdin":"150\n","expect":"How many items? 150\nThat's a big order!\nThank you!","hidden":false},{"name":"Test 2","stdin":"20\n","expect":"How many items? 20\nThank you!","hidden":true}],"require":[],"forbid":[],"more":[{"task":"Write a program that asks for a car's speed and prints `Too fast!` when the speed is over 50. When it isn't, the program prints nothing more. If the user types `62`, the run looks like this:\n\n```\nSpeed: 62\nToo fast!\n```\n","kind":"code","seed":"speed = int(input(\"Speed: \"))\n","solution":"speed = int(input(\"Speed: \"))\nif speed > 50:\n    print(\"Too fast!\")\n","hints":["Use an `if` with the condition `speed > 50`, and end the line with a colon.","The `print` goes on the next line, indented by four spaces, so it belongs to the `if`."],"tests":[{"name":"Test 1","stdin":"62\n","expect":"Speed: 62\nToo fast!","hidden":false},{"name":"Test 2","stdin":"45\n","expect":"Speed: 45","hidden":true},{"name":"Test 3","stdin":"50\n","expect":"Speed: 50","hidden":true}],"require":[],"forbid":[]},{"task":"What does this program print? Type each line it prints.\n","kind":"predict","seed":"points = 12\nif points > 10:\n    print(\"Bonus!\")\n    points = points + 5\nif points > 20:\n    print(\"Super bonus!\")\nprint(points)\n","solution":"points = 12\nif points > 10:\n    print(\"Bonus!\")\n    points = points + 5\nif points > 20:\n    print(\"Super bonus!\")\nprint(points)\n","hints":["12 is greater than 10, so the first block runs: it prints and adds 5 to `points`.","The second condition is checked with the new value of `points`. The last line runs in any case."],"tests":[{"name":"Output","stdin":"","expect":"Bonus!\n17","hidden":false}],"require":[],"forbid":[],"lines":["Bonus!","17"]}]}