''' Purpose: introduce lists ''' import pause ''' # get text and convert to list form reply = input( 'Enter text: ' ) strings = reply.split() print( 'reply =', reply ) print( 'strings =', strings ) print() # get text and convert to list form reply = input( 'Just hit enter: ' ) print() strings = reply.split() print( 'reply =', reply ) print( 'strings =', strings ) pause.enter_when_ready() # get text and convert to list form and print words one by one reply = input( 'Enter text: ' ) strings = reply.split() for s in strings: # s will take on each word in strings print(s) pause.enter_when_ready() # get numeric text and convert to numbers reply = input( 'Enter a list of numbers: ' ) numeric_strings = reply.split() print( 'reply =', reply ) print( 'strings =', strings ) pause.enter_when_ready() total = 0 # accumulator must be initialized before loop for ns in numeric_strings: n = int(ns) total = total + n print( numeric_strings, total ) pause.enter_when_ready() ''' # get numeric text and convert to numbers reply = input( 'Enter a list of numbers: ' ) numeric_strings = reply.split() print( 'reply =', reply ) print( 'numeric_strings =', numeric_strings) pause.enter_when_ready() # Don't use this in your own code. Its for teaching purposes numbers = [] # here the accumulator is not a a number but a list # We are creating numbers as an empty list so that we can later add things to it print( 'numbers: ', numbers ) # parenthesis are used two ways: # 1: After a function # 2: also used as a grouping operator (for math) # Brackets indicate a list for ns in numeric_strings: # each time we go through the loop we have a different string from the list n = int(ns) numbers.append(n) # append function adds the element in the parenthesis to the end of a list print('n = ', n, 'ns =', ns, 'numbers: ', numbers) #n.append(12) numbers can't append only lists can print(numbers) #total = sum(numbers) #print( total )