{"id":"elif-order","slug":"order-of-conditions","title":"The order of conditions","text":"Python checks the branches of a conditional statement from top to bottom and runs only the first one whose condition is true. That makes the order of the conditions important. This program should praise a great result, but it never does:\n\n```python\npoints = 95\nif points >= 50:\n    print(\"Passed\")\nelif points >= 90:\n    print(\"Passed with distinction\")\nelse:\n    print(\"Failed\")\n```\n\n```output\nPassed\n```\n\n95 is at least 50, so the first branch runs and the rest of the statement is skipped. In fact the `elif points >= 90` branch can never run: every number that is 90 or more is also 50 or more, so the first condition always catches it first. The fix is to check the stricter condition first:\n\n```python\npoints = 95\nif points >= 90:\n    print(\"Passed with distinction\")\nelif points >= 50:\n    print(\"Passed\")\nelse:\n    print(\"Failed\")\n```\n\n```output\nPassed with distinction\n```\n\nA good rule for limits like these: with `>=` or `>`, check the limits from the highest down; with `<=` or `<`, from the lowest up.\n\n### Each branch knows what came before\n\nWhen Python gets to `elif points >= 50`, it already knows that `points >= 90` was false. So that branch is really for points from 50 to 89, without having to say so. You don't need to repeat the earlier conditions in the later ones.\n\n### One statement or several\n\nA chain of `if`, `elif` and `else` is one statement, and at most one of its branches runs. Separate `if` statements are each checked on their own, so several of them can run:\n\n```python\nspeed = 85\nif speed > 80:\n    print(\"Fine: 200 euros\")\nif speed > 60:\n    print(\"Fine: 100 euros\")\n```\n\n```output\nFine: 200 euros\nFine: 100 euros\n```\n\nThe driver gets two fines. Change the second `if` into `elif`, and only the first one is printed. Use separate `if` statements when the checks have nothing to do with each other, and one chain when exactly one of the results should come out.\n","task":"Children under 7 get in free, but this program gives a 5-year-old a youth ticket. Fix the order of the conditions:\n\n```\nAge: 5\nFree entry\n```\n","kind":"code","seed":"age = int(input(\"Age: \"))\nif age < 18:\n    print(\"Youth ticket: 6 euros\")\nelif age < 7:\n    print(\"Free entry\")\nelif age >= 65:\n    print(\"Senior ticket: 7 euros\")\nelse:\n    print(\"Adult ticket: 10 euros\")\n","solution":"age = int(input(\"Age: \"))\nif age < 7:\n    print(\"Free entry\")\nelif age < 18:\n    print(\"Youth ticket: 6 euros\")\nelif age >= 65:\n    print(\"Senior ticket: 7 euros\")\nelse:\n    print(\"Adult ticket: 10 euros\")\n","hints":["A 5-year-old is also under 18, so the first branch catches every child before Python ever gets to `age < 7`.","Swap the first two conditions and their blocks: `if age < 7:` first, then `elif age < 18:`."],"tests":[{"name":"Test 1","stdin":"5\n","expect":"Age: 5\nFree entry","hidden":false},{"name":"Test 2","stdin":"12\n","expect":"Age: 12\nYouth ticket: 6 euros","hidden":true},{"name":"Test 3","stdin":"7\n","expect":"Age: 7\nYouth ticket: 6 euros","hidden":true},{"name":"Test 4","stdin":"70\n","expect":"Age: 70\nSenior ticket: 7 euros","hidden":true},{"name":"Test 5","stdin":"30\n","expect":"Age: 30\nAdult ticket: 10 euros","hidden":true}],"require":[],"forbid":[],"more":[{"task":"What does this program print? Type each line it prints.\n","kind":"predict","seed":"score = 72\nif score >= 60:\n    print(\"C\")\nelif score >= 70:\n    print(\"B\")\nif score >= 50:\n    print(\"Passed\")\nelse:\n    print(\"Failed\")\n","solution":"score = 72\nif score >= 60:\n    print(\"C\")\nelif score >= 70:\n    print(\"B\")\nif score >= 50:\n    print(\"Passed\")\nelse:\n    print(\"Failed\")\n","hints":["In the first statement, Python runs only the first branch whose condition is true. 72 is at least 60.","The second `if` starts a new statement, so it's checked on its own."],"tests":[{"name":"Output","stdin":"","expect":"C\nPassed","hidden":false}],"require":[],"forbid":[],"lines":["C","Passed"]},{"task":"Write a program that asks for the wind speed in meters per second (a whole number) and describes it:\n\n- below 1: `Calm`\n- 1 to 5: `Light breeze`\n- 6 to 10: `Moderate wind`\n- 11 to 17: `Strong wind`\n- 18 or more: `Storm`\n\n```\nWind speed: 8\nModerate wind\n```\n","kind":"code","seed":"speed = int(input(\"Wind speed: \"))\n","solution":"speed = int(input(\"Wind speed: \"))\nif speed < 1:\n    print(\"Calm\")\nelif speed <= 5:\n    print(\"Light breeze\")\nelif speed <= 10:\n    print(\"Moderate wind\")\nelif speed <= 17:\n    print(\"Strong wind\")\nelse:\n    print(\"Storm\")\n","hints":["Start from the lowest limit and go up: `if speed < 1:`, then `elif speed <= 5:` and so on.","Each `elif` only runs when all the conditions above it were false, so it doesn't need a lower limit.","The last case, 18 or more, is everything that's left, so it can be the `else`."],"tests":[{"name":"Test 1","stdin":"8\n","expect":"Wind speed: 8\nModerate wind","hidden":false},{"name":"Test 2","stdin":"0\n","expect":"Wind speed: 0\nCalm","hidden":true},{"name":"Test 3","stdin":"1\n","expect":"Wind speed: 1\nLight breeze","hidden":true},{"name":"Test 4","stdin":"5\n","expect":"Wind speed: 5\nLight breeze","hidden":true},{"name":"Test 5","stdin":"6\n","expect":"Wind speed: 6\nModerate wind","hidden":true},{"name":"Test 6","stdin":"10\n","expect":"Wind speed: 10\nModerate wind","hidden":true},{"name":"Test 7","stdin":"11\n","expect":"Wind speed: 11\nStrong wind","hidden":true},{"name":"Test 8","stdin":"17\n","expect":"Wind speed: 17\nStrong wind","hidden":true},{"name":"Test 9","stdin":"18\n","expect":"Wind speed: 18\nStorm","hidden":true},{"name":"Test 10","stdin":"30\n","expect":"Wind speed: 30\nStorm","hidden":true}],"require":[{"pattern":"\\belif\\b","message":"Use one conditional statement with `elif` branches."}],"forbid":[]}]}