{"id":"logic-or","slug":"or","title":"At least one: or","text":"The operator `or` combines two conditions into one that is true when at least one of them is true:\n\n```python\nday = input(\"Day: \")\nif day == \"Saturday\" or day == \"Sunday\":\n    print(\"No school today!\")\nelse:\n    print(\"Time for school.\")\n```\n\n```input\nSunday\n```\n\n```output\nDay: Sunday\nNo school today!\n```\n\nHere are all four combinations:\n\n```pycon\n>>> True or True\nTrue\n>>> True or False\nTrue\n>>> False or True\nTrue\n>>> False or False\nFalse\n```\n\nOnly `False or False` gives `False`. Notice that `or` is also true when both sides are true: it doesn't mean \"one or the other, but not both\".\n\n### Outside a range\n\nWith `or`, you can check that a number is outside a range, below the lower limit or above the upper one:\n\n```python\nnumber = int(input(\"Pick a number from 1 to 10: \"))\nif number < 1 or number > 10:\n    print(\"That's not between 1 and 10!\")\nelse:\n    print(f\"You picked {number}.\")\n```\n\n```input\n12\n```\n\n```output\nPick a number from 1 to 10: 12\nThat's not between 1 and 10!\n```\n\nA number can't be below 1 and above 10 at the same time, so `number < 1 and number > 10` would never be true. When you check for a value outside a range, the operator is `or`.\n\n### A mistake that hides\n\nEach side of `or` must be a whole condition, just as with `and`. This time, though, leaving out part of it doesn't cause an error, which makes the mistake hard to spot:\n\n```python\nday = \"Monday\"\nif day == \"Saturday\" or \"Sunday\":\n    print(\"Weekend!\")\n```\n\n```output\nWeekend!\n```\n\nPython reads the condition as `(day == \"Saturday\") or (\"Sunday\")`. The right side isn't a comparison at all, just a string, and in a condition Python treats any string that isn't empty as true. So the whole condition is always true, whatever the day. The fix is to compare the variable again: `day == \"Saturday\" or day == \"Sunday\"`.\n","task":"Fill in the blank so the program says `Winter month` for December, January and February (months 12, 1 and 2):\n\n```\nMonth (1 to 12): 1\nWinter month\n```\n","kind":"fill","seed":"month = int(input(\"Month (1 to 12): \"))\nif month == 12 [[or]] month == 1 or month == 2:\n    print(\"Winter month\")\nelse:\n    print(\"Not a winter month\")\n","solution":"month = int(input(\"Month (1 to 12): \"))\nif month == 12 or month == 1 or month == 2:\n    print(\"Winter month\")\nelse:\n    print(\"Not a winter month\")\n","hints":["The condition should be true when any one of the three comparisons is true.","The operator for \"at least one\" is `or`."],"tests":[{"name":"Test 1","stdin":"1\n","expect":"Month (1 to 12): 1\nWinter month","hidden":false},{"name":"Test 2","stdin":"12\n","expect":"Month (1 to 12): 12\nWinter month","hidden":true},{"name":"Test 3","stdin":"6\n","expect":"Month (1 to 12): 6\nNot a winter month","hidden":true},{"name":"Test 4","stdin":"2\n","expect":"Month (1 to 12): 2\nWinter month","hidden":true}],"require":[],"forbid":[],"more":[{"task":"A die shows the numbers 1 to 6. Write a program that asks for the number a player rolled. If the number is less than 1 or greater than 6, it prints `A die has no such number.` Otherwise it prints `You rolled` and the number.\n\n```\nYour roll: 7\nA die has no such number.\n```\n","kind":"code","seed":"roll = int(input(\"Your roll: \"))\n","solution":"roll = int(input(\"Your roll: \"))\nif roll < 1 or roll > 6:\n    print(\"A die has no such number.\")\nelse:\n    print(f\"You rolled {roll}\")\n","hints":["The number is wrong when it's below 1 or above 6, so the two comparisons are joined with `or`.","`if roll < 1 or roll > 6:` catches the wrong numbers, and the `else` prints the roll with an f-string."],"tests":[{"name":"Test 1","stdin":"7\n","expect":"Your roll: 7\nA die has no such number.","hidden":false},{"name":"Test 2","stdin":"3\n","expect":"Your roll: 3\nYou rolled 3","hidden":true},{"name":"Test 3","stdin":"0\n","expect":"Your roll: 0\nA die has no such number.","hidden":true},{"name":"Test 4","stdin":"6\n","expect":"Your roll: 6\nYou rolled 6","hidden":true},{"name":"Test 5","stdin":"1\n","expect":"Your roll: 1\nYou rolled 1","hidden":true}],"require":[{"pattern":"\\bor\\b","message":"Check the two limits with `or`."}],"forbid":[]},{"task":"This program should continue when the user types `yes` or `y`, and stop for anything else. But it always continues. Fix the condition.\n\n```\nDo you want to continue? no\nStopping.\n```\n","kind":"code","seed":"answer = input(\"Do you want to continue? \")\nif answer == \"yes\" or \"y\":\n    print(\"Continuing...\")\nelse:\n    print(\"Stopping.\")\n","solution":"answer = input(\"Do you want to continue? \")\nif answer == \"yes\" or answer == \"y\":\n    print(\"Continuing...\")\nelse:\n    print(\"Stopping.\")\n","hints":["The right side of `or` is just the string `\"y\"`, and a string that isn't empty counts as true.","Make the right side a whole comparison: `answer == \"y\"`."],"tests":[{"name":"Test 1","stdin":"no\n","expect":"Do you want to continue? no\nStopping.","hidden":false},{"name":"Test 2","stdin":"yes\n","expect":"Do you want to continue? yes\nContinuing...","hidden":true},{"name":"Test 3","stdin":"y\n","expect":"Do you want to continue? y\nContinuing...","hidden":true},{"name":"Test 4","stdin":"n\n","expect":"Do you want to continue? n\nStopping.","hidden":true}],"require":[],"forbid":[]}]}