''' Purpose: introduce summing accumulation ''' # USE THIS FOR BIG_PRODUCTION.PY (the homework) # get a list of numbers reply = input( 'Enter a list of numbers: ' ) # Example: reply is '1 2 3' slist = reply.split() # Example: slist is now ['1','2','3'] print() # ACCUMULATE LIST OF INTEGER VERSIONS OF STRINGS IN SLIST AND STORE IN NLIST # slist (string list) nlist (number list) print( 'slist is', slist ) # convert numeric text into numbers one by one nlist = [] # initialize holder to prepare for adding elements - accumulator! for s in slist : print( 'Loop variable:', s ) nbr = int( s ) # Convert the strings in slist to integers nlist.append( nbr ) # Add each converted string in nlist (list of integers) print( 'nlist is', nlist ) print() # Keep in mind for the homework you don't want the print statements in the for loop ^ # That's just to show you all how this works! (just for demonstration) # NUMBER ACCUMULATION # by accumulation get the sum of the numbers total = 0 # number accumulators where we add / subtract are initialized to 0 # number accumulations where we muliply/divide are initialized to 1 (*cough* big_production) for nbr in nlist : print( 'Loop variable:', nbr ) total = total + nbr print( 'total so far:', total ) print() # print summation print( "sum(", nlist, "):", total ) # Don't name your variables max, sum, min, etc. because those are keywords for built in functions. # If you do name your variables those keywords, you lose the functionality when you try to use them # as functions! # Ex. # max = 2 # x = max([1,2,3]) # This messes up the max function ^ Don't do that! # Name it something else! # COMMENTING OUT MULTIPLE LINES OF CODE AT ONCE: # To commment out multiple lines of code at once: # Single comment can be commented out using # # Highlight all the lines Cntl / on PC and Windows # Command / on Macs