{"id":"elif-else","slug":"else","title":"Two branches with else","text":"Often a program should do one thing when a condition is true and something else when it's false. With what you know so far, you would write two `if` statements with opposite conditions:\n\n```python\nage = int(input(\"Your age: \"))\nif age < 18:\n    print(\"Youth ticket: 6 euros\")\nif age >= 18:\n    print(\"Adult ticket: 10 euros\")\n```\n\n```input\n15\n```\n\n```output\nYour age: 15\nYouth ticket: 6 euros\n```\n\nThis works, but the two conditions must fit together exactly. Change the 18 in one of them and forget the other, and some ages get two tickets or none at all.\n\n### else\n\nAn **`else` branch** runs when the condition of its `if` is false. It covers every other case, so it needs no condition of its own:\n\n```python\nage = int(input(\"Your age: \"))\nif age < 18:\n    print(\"Youth ticket: 6 euros\")\nelse:\n    print(\"Adult ticket: 10 euros\")\nprint(\"Enjoy the movie!\")\n```\n\n```input\n34\n```\n\n```output\nYour age: 34\nAdult ticket: 10 euros\nEnjoy the movie!\n```\n\n`else` is followed by a colon and its own indented block, and it's at the same indentation as its `if`. Exactly one of the two blocks runs, never both and never neither. Together the `if` and the `else` form one conditional statement, so an `else` can't stand on its own: it always comes right after the block of an `if`.\n\n### Even or odd\n\nA number is even when dividing it by 2 leaves no remainder. Without `else`, telling even and odd numbers apart takes two `if` statements. With `else`, one condition is enough:\n\n```python\nnumber = int(input(\"Type a whole number: \"))\nif number % 2 == 0:\n    print(f\"{number} is even\")\nelse:\n    print(f\"{number} is odd\")\n```\n\n```input\n7\n```\n\n```output\nType a whole number: 7\n7 is odd\n```\n\nThe same idea tells whether one number divides another evenly: `minutes % 60 == 0` is true when the minutes make whole hours.\n\nA common mistake is to give `else` a condition, as if it were another `if`:\n\n```python\ntemperature = 15\nif temperature > 20:\n    print(\"Wear shorts\")\nelse temperature <= 20:\n    print(\"Wear jeans\")\n```\n\n```crash\n  File \"main.py\", line 4\n    else temperature <= 20:\n         ^^^^^^^^^^^\nSyntaxError: expected ':'\n```\n\nPython expects the colon right after `else`. Leave the condition out: `else` already means \"in every other case\".\n","task":"Fill in the blank so the quiz says `Not quite. It's Jupiter.` for any other answer:\n\n```\nWhat is the largest planet? Jupiter\nCorrect!\n```\n","kind":"fill","seed":"answer = input(\"What is the largest planet? \")\nif answer == \"Jupiter\":\n    print(\"Correct!\")\n[[else]]:\n    print(\"Not quite. It's Jupiter.\")\n","solution":"answer = input(\"What is the largest planet? \")\nif answer == \"Jupiter\":\n    print(\"Correct!\")\nelse:\n    print(\"Not quite. It's Jupiter.\")\n","hints":["The second branch should run whenever the condition of the `if` is false.","Type `else` into the blank. The colon after it is already there."],"tests":[{"name":"Test 1","stdin":"Jupiter\n","expect":"What is the largest planet? Jupiter\nCorrect!","hidden":false},{"name":"Test 2","stdin":"Saturn\n","expect":"What is the largest planet? Saturn\nNot quite. It's Jupiter.","hidden":true},{"name":"Test 3","stdin":"jupiter\n","expect":"What is the largest planet? jupiter\nNot quite. It's Jupiter.","hidden":true}],"require":[],"forbid":[],"more":[{"task":"A minibus has rows of 4 seats, and passengers fill the rows from the front. Write a program that asks how many passengers there are. If they fill every row exactly, it prints `Every row is full.` Otherwise it prints how many passengers sit in the last row.\n\n```\nPassengers: 10\nPassengers in the last row: 2\n```\n","kind":"code","seed":"passengers = int(input(\"Passengers: \"))\n","solution":"passengers = int(input(\"Passengers: \"))\nif passengers % 4 == 0:\n    print(\"Every row is full.\")\nelse:\n    print(f\"Passengers in the last row: {passengers % 4}\")\n","hints":["The remainder `passengers % 4` is the number of people left over after the full rows.","When the remainder is 0, every row is full: `if passengers % 4 == 0:`.","In the `else` block, print the remainder with an f-string."],"tests":[{"name":"Test 1","stdin":"10\n","expect":"Passengers: 10\nPassengers in the last row: 2","hidden":false},{"name":"Test 2","stdin":"12\n","expect":"Passengers: 12\nEvery row is full.","hidden":true},{"name":"Test 3","stdin":"5\n","expect":"Passengers: 5\nPassengers in the last row: 1","hidden":true}],"require":[{"pattern":"\\belse\\b","message":"Use an `if` with an `else` branch."}],"forbid":[]},{"task":"An online shop charges 5 euros for shipping, but shipping is free for orders of 50 euros or more. Write a program that asks for the order total (in whole euros) and prints the shipping cost and the total to pay. Store the shipping cost in a variable in each branch, and print both lines after the conditional statement.\n\n```\nOrder total: 30\nShipping: 5 euros\nTo pay: 35 euros\n```\n","kind":"code","seed":"total = int(input(\"Order total: \"))\n","solution":"total = int(input(\"Order total: \"))\nif total >= 50:\n    shipping = 0\nelse:\n    shipping = 5\nprint(f\"Shipping: {shipping} euros\")\nprint(f\"To pay: {total + shipping} euros\")\n","hints":["Shipping is free from 50 euros up, so the condition is `total >= 50`.","Set `shipping = 0` in one branch and `shipping = 5` in the other. Whichever branch runs, the variable has a value afterwards.","The two `print` lines come after the whole `if` and `else`, at the left edge, so they run every time."],"tests":[{"name":"Test 1","stdin":"30\n","expect":"Order total: 30\nShipping: 5 euros\nTo pay: 35 euros","hidden":false},{"name":"Test 2","stdin":"60\n","expect":"Order total: 60\nShipping: 0 euros\nTo pay: 60 euros","hidden":true},{"name":"Test 3","stdin":"50\n","expect":"Order total: 50\nShipping: 0 euros\nTo pay: 50 euros","hidden":true},{"name":"Test 4","stdin":"49\n","expect":"Order total: 49\nShipping: 5 euros\nTo pay: 54 euros","hidden":true}],"require":[{"pattern":"\\belse\\b","message":"Use an `if` with an `else` branch."}],"forbid":[]}]}