{"id":"arithmetic-using-variables","slug":"using-variables","title":"Calculating with variables","text":"A program that works with numbers usually reads them, stores them in variables and then calculates with the variables. This one adds up three days of running, in whole kilometers, and works out the distance per day:\n\n```python\nmonday = int(input(\"Monday, km: \"))\ntuesday = int(input(\"Tuesday, km: \"))\nwednesday = int(input(\"Wednesday, km: \"))\ntotal = monday + tuesday + wednesday\nprint(f\"Distance run: {total} km\")\nprint(f\"Per day: {total / 3} km\")\n```\n\n```input\n5\n8\n2\n```\n\n```output\nMonday, km: 5\nTuesday, km: 8\nWednesday, km: 2\nDistance run: 15 km\nPer day: 5.0 km\n```\n\n### Adding to a variable\n\nAnother way is to keep a running total in one variable and add each new value to it as it comes in:\n\n```python\ntotal = 0\nprice = float(input(\"First item: \"))\ntotal = total + price\nprice = float(input(\"Second item: \"))\ntotal = total + price\nprint(f\"To pay: {total}\")\n```\n\n```input\n3.5\n2.25\n```\n\n```output\nFirst item: 3.5\nSecond item: 2.25\nTo pay: 5.75\n```\n\nAs before, `total = total + price` works out the right side first with the current values, then stores the result back in `total`. The total starts at 0, so the first addition has something to add to. Here one variable, `price`, holds each item in turn: after it's added to the total, its old value isn't needed any more.\n\n### The shorthand +=\n\nIncreasing a variable is so common that Python has a shorthand: `total += price` means the same as `total = total + price`. There are also `-=` and `*=`, and they work the same way:\n\n```python\nbalance = 100\nbalance += 25\nbalance -= 40\nbalance *= 2\nprint(balance)\n```\n\n```output\n170\n```\n\n100 plus 25 is 125, minus 40 is 85, and doubled that's 170.\n\n### Keep the values you need\n\nReusing one variable is fine for a running total. But if the program needs every value at the end, each one needs a variable of its own:\n\n```python\nmorning = int(input(\"Temperature in the morning: \"))\nevening = int(input(\"Temperature in the evening: \"))\nprint(f\"From {morning} to {evening} degrees: a change of {evening - morning}\")\n```\n\n```input\n12\n19\n```\n\n```output\nTemperature in the morning: 12\nTemperature in the evening: 19\nFrom 12 to 19 degrees: a change of 7\n```\n\nWhen you do reuse a variable, keep it for values that play the same part, like the prices above. A variable `value` that first holds the number of guests and later the price of a ticket would work, but the reader would have to work out what it means on every line. `guests` and `ticket_price` say it at once.\n","task":"Fill in the blanks so the program adds both answers to `total`. If the user types `3` and `5`, the run looks like this:\n\n```\nApples: 3\nPears: 5\nFruits: 8\n```\n","kind":"fill","seed":"total = 0\ntotal [[+=]] int(input(\"Apples: \"))\ntotal [[+=]] int(input(\"Pears: \"))\nprint(f\"Fruits: {total}\")\n","solution":"total = 0\ntotal += int(input(\"Apples: \"))\ntotal += int(input(\"Pears: \"))\nprint(f\"Fruits: {total}\")\n","hints":["Each line should add the number the user types to `total`.","The shorthand for adding to a variable is `+=`."],"tests":[{"name":"Test 1","stdin":"3\n5\n","expect":"Apples: 3\nPears: 5\nFruits: 8","hidden":false},{"name":"Test 2","stdin":"10\n0\n","expect":"Apples: 10\nPears: 0\nFruits: 10","hidden":true}],"require":[],"forbid":[],"more":[{"task":"Write a program that asks for three test scores (whole numbers) and prints their total and their mean. If the user types `7`, `9` and `8`, the run looks like this:\n\n```\nScore 1: 7\nScore 2: 9\nScore 3: 8\nTotal: 24\nMean: 8.0\n```\n","kind":"code","seed":"# Ask for the three scores, then print the total and the mean.\n","solution":"# Ask for the three scores, then print the total and the mean.\nscore1 = int(input(\"Score 1: \"))\nscore2 = int(input(\"Score 2: \"))\nscore3 = int(input(\"Score 3: \"))\ntotal = score1 + score2 + score3\nprint(f\"Total: {total}\")\nprint(f\"Mean: {total / 3}\")\n","hints":["Read each score with `int(input(...))`, into a variable of its own or straight into a running total.","The mean is the total divided by 3. With `/`, it's a float, so it's printed as `8.0`."],"tests":[{"name":"Test 1","stdin":"7\n9\n8\n","expect":"Score 1: 7\nScore 2: 9\nScore 3: 8\nTotal: 24\nMean: 8.0","hidden":false},{"name":"Test 2","stdin":"10\n5\n3\n","expect":"Score 1: 10\nScore 2: 5\nScore 3: 3\nTotal: 18\nMean: 6.0","hidden":true},{"name":"Test 3","stdin":"1\n2\n2\n","expect":"Score 1: 1\nScore 2: 2\nScore 3: 2\nTotal: 5\nMean: 1.6666666666666667","hidden":true}],"require":[],"forbid":[]},{"task":"Write a program for a cash register. It asks for the prices of three items, which may have decimals, and prints the running total after each one. Keep the total in one variable and add each price to it with `+=`. If the user types `2.5`, `4` and `1.25`, the run looks like this:\n\n```\nItem 1: 2.5\nTotal so far: 2.5\nItem 2: 4\nTotal so far: 6.5\nItem 3: 1.25\nTotal so far: 7.75\n```\n","kind":"code","seed":"total = 0\n","solution":"total = 0\ntotal += float(input(\"Item 1: \"))\nprint(f\"Total so far: {total}\")\ntotal += float(input(\"Item 2: \"))\nprint(f\"Total so far: {total}\")\ntotal += float(input(\"Item 3: \"))\nprint(f\"Total so far: {total}\")\n","hints":["Read a price, add it to the total, print the total: then the same again, twice.","The prices may have decimals, so read them with `float`.","`total += float(input(\"Item 1: \"))` reads the first price and adds it in one line."],"tests":[{"name":"Test 1","stdin":"2.5\n4\n1.25\n","expect":"Item 1: 2.5\nTotal so far: 2.5\nItem 2: 4\nTotal so far: 6.5\nItem 3: 1.25\nTotal so far: 7.75","hidden":false},{"name":"Test 2","stdin":"10\n0.5\n3\n","expect":"Item 1: 10\nTotal so far: 10.0\nItem 2: 0.5\nTotal so far: 10.5\nItem 3: 3\nTotal so far: 13.5","hidden":true}],"require":[{"pattern":"\\+=","min":3,"message":"Add each price to `total` with `+=`."}],"forbid":[]}]}