''' Purpose: introduce summing accumulation ''' # get a list of numbers reply = input( 'Enter a list of numbers: ' ) slist = reply.split() # slist is a list of numeric strings print() print( 'slist =', slist ) print() # convert numeric text into numbers one by one nlist = [] # initialize holder to prepare for adding elements # for each numeric string (s) in the list slist for s in slist : nbr = int( s ) # cast the numeric string into the type int nlist.append( nbr ) # append the int version into the list accumulator nlist print( 'nlist =', nlist ) # don't modify a list that you're going through in a for loop inside that for loop # it will go on forever and gives you errors # here, we want a new list for the list of numbers instead of modifying slist directly # also it makes more sense if we have a new list because we use the two lists for different # purposes # by accumulation get the sum of the numbers total = 0 # accumulator for a summation problem always starts with 0 # for each number # (we named it number - doesn't matter what you name it) # in our list of numbers nlist for number in nlist : total = total + number # add to our running total # you can reuse loop variable! might mean different things between different for loops # so use different variables for each for loop # print summation print( "sum(", nlist, "):", total ) # accumulator for a product problem always starts with 1 NOT 0!!! # x * 0 = 0 always