Ask coding questions

← Back to all posts
How do I keep a running total in a function in python 3?

Hello, does anyone know why I cannot maintain a running total? I am trying to get the user to input weights of a bag and then once they've done that the bags_checked for that individual user goes up by 1 and the total_bags_checked goes up by one too and I want to repeat this so next time a user inputs their weight the total is the previous one plus 1 (eg 2 + 1 = 3). Eventually, I want to put this​ in a menu so it will continue to repeat so I need the total to now restart from 0 also as I am going to display the total_bags_checked later on in my program. Although, the total always restarts at zero so if I run the function twice the total is 1. So does anyone know what I can do to fix this so I can keep the totals stored for the next user to increase the value by 1? I hope this makes sense, I'm not too sure how to explain it. Also, I put the function​ 'process' at line 7 although I put it before at line 71 although it caused an error when I called 'checking_weight' so instead I called 'process​' at line 71. Does anyone know why it wouldn't work​ when defined at line 71? (My main problem is the previous one mentioned). Thanks. (I'm using python 3).

link to code:

my code (line 7 - 20):

coins_needed = 0
bags_checked = 0
total_bags_checked = 0

def process(bags_checked, total_bags_checked):
   bags_checked += 1
   total_bags_checked += 1

   print ("bags checked ", bags_checked)
   print (volunteers_names)
   print (list_of_bags_checked)
   print ("total", total_bags_checked)

   return bags_checked
   return total_bags_checked

Firstly, you can't return two things, so try packing and unpacking (just fancy terms that mean using an array to do things with multiple values). What I mean is

def f(x, y):
  return x,y

x, y = 1, 2
x, y = f(x, y)
print(x, y) # => 2, 2

Also, though this is generally a bad practice, do you know about globals? That just means a function can access "global" variables, which means that any change to them in the function itself will change them everywhere in the program. Example:

x = 0
def f():
  global x
  x += 1

f() # => 1
f() # => 2

HINT: # => means it will output (at least that's what it means to me >.<)


@a5rocks hi, thank you for your reply. I understand what is meant by global variables and such but could you put/explain your answer code in terms of my program. I'm having difficulty on how I would implement this myself. Sorry, it takes me a bit longer to fully grasp a concept. Thanks.


@havana Sorry for the late reply xD. Anyway:

total_bags = 0
user_bags = {}
def process(user):
  global user_bags
  global total_bags
  if not user_bags.get(user):
    user_bags[user] = 0
  weight = input('How much does your bag weigh? ') #i don't know what "weight" is used for
  total_bags += 1
  user_bags[user] += 1

Where then you just call process(user_id or whatever other identifier you have).


@a5rocks hi. Thanks for your help! It works, just one last thing if you don't mind. Could you try and explain/say what you did to make it work because as well as getting it to work I want to understand it so I can learn from my mistakes and next time know what to do myself instead of asking the same questions? Thank you so much, btw, I upvoted you and you've been extremely helpful. I hope one day you are in the top contributors!!


@havana Sorry for the late reply >.<

Anyway, here's a commented version :D

total_bags = 0
user_bags = {}
def process(user):
  global user_bags # Declare "user_bags" as a global variable :
  global total_bags # Same as above
  if not user_bags.get(user): # If the "user" key doesn't exist in the user_bags dictionary
    user_bags[user] = 0 # Making it appear so that we can just add numbers.
  weight = input('How much does your bag weigh? ') #i don't know what "weight" is used for
  total_bags += 1 # Increment the counter
  user_bags[user] += 1 # Increment the amount of bags registered by a user.

Now I probably didn't explain user_bags very well, but here, have a (probably) good intro to dictionaries : . That might make you able to tell what the code is doing even with my bad comments.


@a5rocks wow thank you. I've read up on dictionaries and it seems like the thing that I need to do in my code. I want to keep a running total of bags checked and the users accuracy and output it at the end (in a table or in a formatting style). Do you reccomend I do this in a dictionary? I will have multiple users. If you want to see my updated code:


@havana Well there's this very cool module in python called pprint. It's included in the standard library, and all it does is it "pretty prints" whatever you give it. I recommend it if you are looking at dictionaries (and even sometimes lists).

Example :

from pprint import pprint
dictionary = {...something...}


As for if you want to have more than two things next to each other (e.g. a key and a value and another value), you can either just have an dictionary like this:

dictionary = {
  'key': ['value1', 'value2']

or you can check out either the csv module, or maybe check out SQL (which I think might a little overkill for this project).

The good thing about using the csv module is that you can open the output in Excel.

Though, if you just want to stay in the console, I would recommend checking out string formatting in python and/or the pprint module / function.