{"id":"simple-loops-helper-variables","slug":"helper-variables","title":"Helper variables: counting and summing","text":"Variables that keep track of something while a loop runs are called **helper variables**. A typical one counts things, and another adds up a total:\n\n```python\nitems = 0\ntotal = 0\nwhile True:\n    price = int(input(\"Price (0 to finish): \"))\n    if price == 0:\n        break\n    items += 1\n    total += price\nprint(f\"{items} items, total {total} euros\")\n```\n\n```input\n3\n12\n5\n0\n```\n\n```output\nPrice (0 to finish): 3\nPrice (0 to finish): 12\nPrice (0 to finish): 5\nPrice (0 to finish): 0\n3 items, total 20 euros\n```\n\nBoth helpers get their starting value, 0, before the loop, and each iteration updates them. The 0 that ends the loop isn't counted, because the `break` comes before the updates.\n\n### Start before the loop\n\nWhere the starting value is set matters. If it is set inside the loop, the helper starts over on every iteration:\n\n```python\nwhile True:\n    count = 0\n    word = input(\"Word (stop to finish): \")\n    if word == \"stop\":\n        break\n    count += 1\nprint(f\"You typed {count} words.\")\n```\n\n```input\nsun\nmoon\nstop\n```\n\n```output\nWord (stop to finish): sun\nWord (stop to finish): moon\nWord (stop to finish): stop\nYou typed 0 words.\n```\n\nOn the last iteration, `count = 0` runs once more just before the `break`, so the count is lost. Move that line above `while True:`, and the program says 2.\n\n### Remembering how the loop ended\n\nA loop can have more than one `break`. A helper variable can remember which one ended it, so that the code after the loop knows what happened. This minibus has 3 seats. It leaves when every seat is taken, or earlier, when nobody else is waiting:\n\n```python\nseats = 3\non_board = 0\nwhile True:\n    name = input(\"Passenger (end when nobody is left): \")\n    if name == \"end\":\n        full = False\n        break\n    on_board += 1\n    print(f\"Welcome aboard, {name}!\")\n    if on_board == seats:\n        full = True\n        break\nif full:\n    print(\"The bus is full. Next stop!\")\nelse:\n    print(f\"The bus leaves with {on_board} passengers.\")\n```\n\n```input\nAda\nBen\nend\n```\n\n```output\nPassenger (end when nobody is left): Ada\nWelcome aboard, Ada!\nPassenger (end when nobody is left): Ben\nWelcome aboard, Ben!\nPassenger (end when nobody is left): end\nThe bus leaves with 2 passengers.\n```\n\nWith a third name instead of `end`, the second `break` would end the loop, with `full` set to `True`. `full` holds `True` or `False`, so it can be the condition of an `if` all by itself.\n","task":"Fill in the blanks, so the program adds up the minutes of exercise the user types:\n\n```\nMinutes of exercise (0 to finish): 30\nMinutes of exercise (0 to finish): 45\nMinutes of exercise (0 to finish): 0\nTotal: 75 minutes\n```\n","kind":"fill","seed":"total = [[0]]\nwhile True:\n    minutes = int(input(\"Minutes of exercise (0 to finish): \"))\n    if minutes == 0:\n        break\n    total [[+=]] minutes\nprint(f\"Total: {total} minutes\")\n","solution":"total = 0\nwhile True:\n    minutes = int(input(\"Minutes of exercise (0 to finish): \"))\n    if minutes == 0:\n        break\n    total += minutes\nprint(f\"Total: {total} minutes\")\n","hints":["Before the first number, nothing has been added yet.","The total starts at `0`, and `total += minutes` adds each number to it."],"tests":[{"name":"Test 1","stdin":"30\n45\n0\n","expect":"Minutes of exercise (0 to finish): 30\nMinutes of exercise (0 to finish): 45\nMinutes of exercise (0 to finish): 0\nTotal: 75 minutes","hidden":false},{"name":"Test 2","stdin":"0\n","expect":"Minutes of exercise (0 to finish): 0\nTotal: 0 minutes","hidden":true},{"name":"Test 3","stdin":"10\n20\n15\n5\n0\n","expect":"Minutes of exercise (0 to finish): 10\nMinutes of exercise (0 to finish): 20\nMinutes of exercise (0 to finish): 15\nMinutes of exercise (0 to finish): 5\nMinutes of exercise (0 to finish): 0\nTotal: 50 minutes","hidden":true}],"require":[],"forbid":[],"more":[{"task":"Write a program that reads daily temperatures until the user types `end`, and then prints how many of the days were below zero, out of all the days.\n\n```\nTemperature: 3\nTemperature: -2\nTemperature: -7\nTemperature: 1\nTemperature: end\nFrosty days: 2 of 4\n```\n","kind":"code","seed":"# Write your program below this line.\n","solution":"# Write your program below this line.\ndays = 0\nfrosty = 0\nwhile True:\n    text = input(\"Temperature: \")\n    if text == \"end\":\n        break\n    temperature = int(text)\n    days += 1\n    if temperature < 0:\n        frosty += 1\nprint(f\"Frosty days: {frosty} of {days}\")\n","hints":["Read the input as a string first and compare it with `\"end\"`. Only after that, convert it with `int(...)`.","You need two helper variables, both starting at 0 before the loop. One counts every day, the other only the days below zero.","Inside the loop, `days += 1` runs for every temperature, and `frosty += 1` only inside `if temperature < 0:`."],"tests":[{"name":"Test 1","stdin":"3\n-2\n-7\n1\nend\n","expect":"Temperature: 3\nTemperature: -2\nTemperature: -7\nTemperature: 1\nTemperature: end\nFrosty days: 2 of 4","hidden":false},{"name":"Test 2","stdin":"5\nend\n","expect":"Temperature: 5\nTemperature: end\nFrosty days: 0 of 1","hidden":true},{"name":"Test 3","stdin":"-1\n-1\n-1\nend\n","expect":"Temperature: -1\nTemperature: -1\nTemperature: -1\nTemperature: end\nFrosty days: 3 of 3","hidden":true},{"name":"Test 4","stdin":"0\n12\n-3\nend\n","expect":"Temperature: 0\nTemperature: 12\nTemperature: -3\nTemperature: end\nFrosty days: 1 of 3","hidden":true}],"require":[],"forbid":[]},{"task":"Write a program for a savings goal of 100 euros. It keeps asking for deposits and prints the amount saved after each one. As soon as the savings reach at least 100, it prints how many deposits it took.\n\n```\nDeposit: 40\nSaved so far: 40\nDeposit: 35\nSaved so far: 75\nDeposit: 30\nSaved so far: 105\nGoal reached! Deposits made: 3\n```\n","kind":"code","seed":"# Write your program below this line.\n","solution":"# Write your program below this line.\nsaved = 0\ndeposits = 0\nwhile True:\n    deposit = int(input(\"Deposit: \"))\n    saved += deposit\n    deposits += 1\n    print(f\"Saved so far: {saved}\")\n    if saved >= 100:\n        break\nprint(f\"Goal reached! Deposits made: {deposits}\")\n","hints":["Two helper variables start at 0 before the loop: the amount saved and the number of deposits.","Each iteration reads a deposit, adds it to the savings, counts it and prints the amount saved.","The check `if saved >= 100: break` comes after the `print`, so the last amount is printed too."],"tests":[{"name":"Test 1","stdin":"40\n35\n30\n","expect":"Deposit: 40\nSaved so far: 40\nDeposit: 35\nSaved so far: 75\nDeposit: 30\nSaved so far: 105\nGoal reached! Deposits made: 3","hidden":false},{"name":"Test 2","stdin":"100\n","expect":"Deposit: 100\nSaved so far: 100\nGoal reached! Deposits made: 1","hidden":true},{"name":"Test 3","stdin":"10\n20\n30\n39\n1\n","expect":"Deposit: 10\nSaved so far: 10\nDeposit: 20\nSaved so far: 30\nDeposit: 30\nSaved so far: 60\nDeposit: 39\nSaved so far: 99\nDeposit: 1\nSaved so far: 100\nGoal reached! Deposits made: 5","hidden":true}],"require":[],"forbid":[]}]}