{"id":"conditionals-using","slug":"conditions-at-work","title":"Conditions at work","text":"A block can hold any code: calculations, assignments and prints. A common pattern changes a variable only in some cases, and then carries on with whatever the variable holds:\n\n```python\nprice = float(input(\"Price: \"))\nif price > 100:\n    price = price * 0.9\n    print(\"You get a 10% discount.\")\nprint(f\"To pay: {price}\")\n```\n\n```input\n150\n```\n\n```output\nPrice: 150\nYou get a 10% discount.\nTo pay: 135.0\n```\n\nWith a price of 80, the block is skipped, `price` stays 80.0, and the program prints just `To pay: 80.0` after the input.\n\n### Several ifs in a row\n\nEach `if` is checked on its own, so when several conditions are true, several blocks run:\n\n```python\nheight = int(input(\"Height in cm: \"))\nif height >= 100:\n    print(\"You can ride the carousel.\")\nif height >= 120:\n    print(\"You can ride the bumper cars.\")\nif height >= 140:\n    print(\"You can ride the roller coaster.\")\nprint(\"Have fun!\")\n```\n\n```input\n125\n```\n\n```output\nHeight in cm: 125\nYou can ride the carousel.\nYou can ride the bumper cars.\nHave fun!\n```\n\n### When one block changes what the next one checks\n\nThe `if` statements run in order, and a later condition sees the variables as the earlier blocks left them. That can catch you out. At this café, an order of 8 or more gets 2 off, and an order of 10 or more also comes with a free cookie. The program looks right, but try it with an order of 11:\n\n```python\ntotal = int(input(\"Order total: \"))\nif total >= 8:\n    total = total - 2\nif total >= 10:\n    print(\"A free cookie comes with your order.\")\nprint(f\"To pay: {total}\")\n```\n\n```input\n11\n```\n\n```output\nOrder total: 11\nTo pay: 9\n```\n\nThe order was 11, so the customer should get the cookie. But the first block has already lowered `total` to 9, and the second condition checks that new value. One fix is to keep the price after the discount in a variable of its own, so every condition checks the order as the customer made it:\n\n```python\ntotal = int(input(\"Order total: \"))\nto_pay = total\nif total >= 8:\n    to_pay = total - 2\nif total >= 10:\n    print(\"A free cookie comes with your order.\")\nprint(f\"To pay: {to_pay}\")\n```\n\n```input\n11\n```\n\n```output\nOrder total: 11\nA free cookie comes with your order.\nTo pay: 9\n```\n\nSwapping the two `if` statements would fix this program too. The variable of its own is the safer habit: every check later in the program still sees the order the customer made.\n","task":"Orders of 50 or more get 5 off. Fill in the blank so the program takes the discount off the total. If the user types `60`, the run looks like this:\n\n```\nOrder total: 60\nTo pay: 55\n```\n","kind":"fill","seed":"total = int(input(\"Order total: \"))\nif total >= 50:\n    total = [[total - 5]]\nprint(f\"To pay: {total}\")\n","solution":"total = int(input(\"Order total: \"))\nif total >= 50:\n    total = total - 5\nprint(f\"To pay: {total}\")\n","hints":["The new total is the old total minus 5.","Build the new value from the old one: `total - 5`."],"tests":[{"name":"Test 1","stdin":"60\n","expect":"Order total: 60\nTo pay: 55","hidden":false},{"name":"Test 2","stdin":"40\n","expect":"Order total: 40\nTo pay: 40","hidden":true},{"name":"Test 3","stdin":"50\n","expect":"Order total: 50\nTo pay: 45","hidden":true}],"require":[],"forbid":[],"more":[{"task":"A fitness app gives badges for the steps walked in a day: a bronze badge for 5000 steps or more, a silver badge for 10000 or more and a gold badge for 20000 or more. A walker gets every badge they reach. Write a program that asks for the steps, prints each badge earned and then `Keep walking!`. If the user types `12000`, the run looks like this:\n\n```\nSteps today: 12000\nBronze badge\nSilver badge\nKeep walking!\n```\n","kind":"code","seed":"steps = int(input(\"Steps today: \"))\n","solution":"steps = int(input(\"Steps today: \"))\nif steps >= 5000:\n    print(\"Bronze badge\")\nif steps >= 10000:\n    print(\"Silver badge\")\nif steps >= 20000:\n    print(\"Gold badge\")\nprint(\"Keep walking!\")\n","hints":["Write one `if` for each badge. Each one is checked on its own, so all the badges the walker reached are printed.","\"5000 or more\" is `steps >= 5000`.","`Keep walking!` is printed every time, so it goes after the blocks, at the left edge."],"tests":[{"name":"Test 1","stdin":"12000\n","expect":"Steps today: 12000\nBronze badge\nSilver badge\nKeep walking!","hidden":false},{"name":"Test 2","stdin":"3000\n","expect":"Steps today: 3000\nKeep walking!","hidden":true},{"name":"Test 3","stdin":"25000\n","expect":"Steps today: 25000\nBronze badge\nSilver badge\nGold badge\nKeep walking!","hidden":true},{"name":"Test 4","stdin":"5000\n","expect":"Steps today: 5000\nBronze badge\nKeep walking!","hidden":true}],"require":[],"forbid":[]},{"task":"In this game, a player with 100 points or more goes up a level, and the 100 points are used up. A player with 150 points or more also finds a bonus chest. But a player with 160 points goes up a level and finds no chest. Fix the program, so that a run looks like this:\n\n```\nPoints: 160\nYou found a bonus chest!\nLevel 2 with 60 points left\n```\n","kind":"code","seed":"points = int(input(\"Points: \"))\nlevel = 1\nif points >= 100:\n    level = level + 1\n    points = points - 100\nif points >= 150:\n    print(\"You found a bonus chest!\")\nprint(f\"Level {level} with {points} points left\")\n","solution":"points = int(input(\"Points: \"))\nlevel = 1\nstart = points\nif points >= 100:\n    level = level + 1\n    points = points - 100\nif start >= 150:\n    print(\"You found a bonus chest!\")\nprint(f\"Level {level} with {points} points left\")\n","hints":["With 160 points, the first block leaves 60 in `points`. What does the second condition see then?","Keep the points the player started with in a variable of their own, such as `start = points`, before the first `if`, and check that one for the chest.","Another fix is to move the chest's `if` statement above the level's. Try your program with 160, with 150 and with 120."],"tests":[{"name":"Test 1","stdin":"160\n","expect":"Points: 160\nYou found a bonus chest!\nLevel 2 with 60 points left","hidden":false},{"name":"Test 2","stdin":"120\n","expect":"Points: 120\nLevel 2 with 20 points left","hidden":true},{"name":"Test 3","stdin":"150\n","expect":"Points: 150\nYou found a bonus chest!\nLevel 2 with 50 points left","hidden":true},{"name":"Test 4","stdin":"90\n","expect":"Points: 90\nLevel 1 with 90 points left","hidden":true},{"name":"Test 5","stdin":"260\n","expect":"Points: 260\nYou found a bonus chest!\nLevel 2 with 160 points left","hidden":true}],"require":[],"forbid":[]}]}