{"id":"logic-and","slug":"and","title":"Both at once: and","text":"Sometimes one condition isn't enough. A roller coaster might let you ride only if you are at least 10 years old and also at least 140 cm tall. The **logical operator** `and` combines two conditions into one, which is true only when both of them are true:\n\n```python\nage = int(input(\"Age: \"))\nheight = int(input(\"Height in cm: \"))\nif age >= 10 and height >= 140:\n    print(\"Enjoy the ride!\")\nelse:\n    print(\"Sorry, you can't ride yet.\")\n```\n\n```input\n12\n135\n```\n\n```output\nAge: 12\nHeight in cm: 135\nSorry, you can't ride yet.\n```\n\nThe rider is old enough but not tall enough, so the whole condition is false. The interpreter shows all four combinations:\n\n```pycon\n>>> True and True\nTrue\n>>> True and False\nFalse\n>>> False and True\nFalse\n>>> False and False\nFalse\n```\n\nOnly `True and True` gives `True`. You can also join more than two conditions: `a and b and c` is true only when all three are.\n\n### Between two limits\n\nThe most common use of `and` is to check that a number is in a range. Here `hour >= 9 and hour < 17` is true for the hours 9 to 16:\n\n```python\nhour = int(input(\"Hour (0 to 23): \"))\nif hour >= 9 and hour < 17:\n    print(\"The shop is open.\")\nelse:\n    print(\"The shop is closed.\")\n```\n\n```input\n18\n```\n\n```output\nHour (0 to 23): 18\nThe shop is closed.\n```\n\n### Each side is a whole condition\n\nIn English you could say \"if the hour is at least 9 and less than 17\", and leave out the second \"the hour\". Python can't fill in the missing part:\n\n```python\nhour = 10\nif hour >= 9 and < 17:\n    print(\"The shop is open.\")\n```\n\n```crash\n  File \"main.py\", line 2\n    if hour >= 9 and < 17:\n                     ^\nSyntaxError: invalid syntax\n```\n\nAfter `and`, Python expects a whole new condition, but it finds `<`. Write the variable again: `hour >= 9 and hour < 17`.\n","task":"Fill in the blank so the program says the water is perfect for swimming when it's from 24 to 30 degrees, limits included:\n\n```\nWater temperature: 26\nPerfect for swimming!\n```\n","kind":"fill","seed":"temperature = int(input(\"Water temperature: \"))\nif temperature >= 24 [[and]] temperature <= 30:\n    print(\"Perfect for swimming!\")\nelse:\n    print(\"Maybe another day.\")\n","solution":"temperature = int(input(\"Water temperature: \"))\nif temperature >= 24 and temperature <= 30:\n    print(\"Perfect for swimming!\")\nelse:\n    print(\"Maybe another day.\")\n","hints":["Both conditions must be true at the same time.","The operator that needs both sides to be true is `and`."],"tests":[{"name":"Test 1","stdin":"26\n","expect":"Water temperature: 26\nPerfect for swimming!","hidden":false},{"name":"Test 2","stdin":"20\n","expect":"Water temperature: 20\nMaybe another day.","hidden":true},{"name":"Test 3","stdin":"24\n","expect":"Water temperature: 24\nPerfect for swimming!","hidden":true},{"name":"Test 4","stdin":"30\n","expect":"Water temperature: 30\nPerfect for swimming!","hidden":true},{"name":"Test 5","stdin":"31\n","expect":"Water temperature: 31\nMaybe another day.","hidden":true}],"require":[],"forbid":[],"more":[{"task":"Write a program that asks for a username and a password. It welcomes the user only when the username is `robin` and the password is `hood123`. In every other case it prints `Wrong username or password.`\n\n```\nUsername: robin\nPassword: hood123\nWelcome, robin!\n```\n","kind":"code","seed":"username = input(\"Username: \")\npassword = input(\"Password: \")\n","solution":"username = input(\"Username: \")\npassword = input(\"Password: \")\nif username == \"robin\" and password == \"hood123\":\n    print(f\"Welcome, {username}!\")\nelse:\n    print(\"Wrong username or password.\")\n","hints":["Both the username and the password must match, so join the two comparisons with `and`.","`username == \"robin\" and password == \"hood123\"`. Everything else goes to the `else`."],"tests":[{"name":"Test 1","stdin":"robin\nhood123\n","expect":"Username: robin\nPassword: hood123\nWelcome, robin!","hidden":false},{"name":"Test 2","stdin":"robin\nhood321\n","expect":"Username: robin\nPassword: hood321\nWrong username or password.","hidden":true},{"name":"Test 3","stdin":"marian\nhood123\n","expect":"Username: marian\nPassword: hood123\nWrong username or password.","hidden":true},{"name":"Test 4","stdin":"Robin\nhood123\n","expect":"Username: Robin\nPassword: hood123\nWrong username or password.","hidden":true}],"require":[{"pattern":"\\band\\b","message":"Check both at once with `and`."}],"forbid":[]},{"task":"A climbing course ends with three tests, each scored from 0 to 10 points. A climber gets the certificate only if every test has at least 5 points and the three scores add up to at least 20. Write a program that asks for the three scores and prints `Certificate earned!` or `No certificate this time.`\n\n```\nTest 1: 8\nTest 2: 5\nTest 3: 9\nCertificate earned!\n```\n","kind":"code","seed":"first = int(input(\"Test 1: \"))\nsecond = int(input(\"Test 2: \"))\nthird = int(input(\"Test 3: \"))\n","solution":"first = int(input(\"Test 1: \"))\nsecond = int(input(\"Test 2: \"))\nthird = int(input(\"Test 3: \"))\ntotal = first + second + third\nif first >= 5 and second >= 5 and third >= 5 and total >= 20:\n    print(\"Certificate earned!\")\nelse:\n    print(\"No certificate this time.\")\n","hints":["The certificate needs four things at once: three scores of at least 5 each, and a total of at least 20.","Work out the total first and store it: `total = first + second + third`.","Join the four comparisons with `and`, starting `first >= 5 and second >= 5 and ...`. Every other case goes to the `else`."],"tests":[{"name":"Test 1","stdin":"8\n5\n9\n","expect":"Test 1: 8\nTest 2: 5\nTest 3: 9\nCertificate earned!","hidden":false},{"name":"Test 2","stdin":"10\n10\n4\n","expect":"Test 1: 10\nTest 2: 10\nTest 3: 4\nNo certificate this time.","hidden":true},{"name":"Test 3","stdin":"5\n5\n9\n","expect":"Test 1: 5\nTest 2: 5\nTest 3: 9\nNo certificate this time.","hidden":true},{"name":"Test 4","stdin":"5\n5\n10\n","expect":"Test 1: 5\nTest 2: 5\nTest 3: 10\nCertificate earned!","hidden":true},{"name":"Test 5","stdin":"4\n8\n8\n","expect":"Test 1: 4\nTest 2: 8\nTest 3: 8\nNo certificate this time.","hidden":true},{"name":"Test 6","stdin":"7\n7\n6\n","expect":"Test 1: 7\nTest 2: 7\nTest 3: 6\nCertificate earned!","hidden":true},{"name":"Test 7","stdin":"9\n3\n10\n","expect":"Test 1: 9\nTest 2: 3\nTest 3: 10\nNo certificate this time.","hidden":true}],"require":[{"pattern":"\\band\\b","message":"Join the conditions with `and`."}],"forbid":[]}]}