""" A simple module showing off for iterators Author: Walker M. White (wmw2) Date: September 28, 2017 (Python 3 Version) """ def simple_iter(seq): """ Prints out the first four elements of the sequence The function uses an iterator to traverse the sequence. If the sequence has less than four elements, there will be an error. Parameter seq: the sequence to print out Precondition: seq a sequence (string or list) """ theiter = iter(seq) x = next(theiter) print('Starting iterator') print(x) x = next(theiter) print(x) x = next(theiter) print(x) x = next(theiter) print(x) print('Done with iterator') def sum(thelist): """ Returns: the sum of all elements in thelist Parameter the list: the list to sum Precondition: thelist is a list of numbers (either floats or ints)""" result = 0 theiter = iter(thelist) for x in theiter: result = result+x print('Adding '+str(x)+' yields '+str(result)) return result