''' Purpose: another take on nested loops ''' # make random module resources available import random # create a table where the element of the rth row in column c is a random # value from the range( 10, 100 ) # set dimensions of a table reply = input( "Enter number of rows: " ) rows = int( reply ) # This is a number! reply = input( "Enter number of columns: " ) columns = int( reply ) # This is a number! # ask for and set the and seed for the random number generation reply = input( "Enter a word: " ) word = reply.strip() # This is a string (whitespace removed) random.seed( word ) # Remember random.seed? It # influences the random results so that the same output # is produced for this particular seed each time. # when you run it, the values with a particular seed # will be the SAME output as ours. print() #### show that a simple range-based loop # print the row indices of the table for r in range( 0, rows ) : # r is the loop variable # it changes value each time we go through the loop # goes from 0 to rows - 1 print( "row:", r ) print() #### show that a nested range-based loop # print the individual row and column indices into the table for r in range( 0, rows ) : # Outer loop for c in range( 0, columns ) : # Inner Loop print( "row:", r, "column:", c ) print() # Basically in each iteration (each time we run through # the loop) of the outer loop, we run the inner loop # and do everything in the inner loop before we # go back up and do the next iteration (next value) of # r in the outer loop. # What is this loop doing? Loops through each row # and then goes through each column value # and prints the row and column number. print() # create the table table = [] # create an empty table. it is our row # accumulator # How do we set up a list accumulator? # EMPTY LIST!! [] # Why do we have a rows = []? We are building a LIST (table) of LISTS (the # row lists. So basically what we're doing is going through each r value # (do it for the number of rows we have) and have a list we wanna build up # and then we go through and append random numbers for the number # of columns we have. Once the row list is made once the c loop finishes # we append it to the bigger table [] list so that the table list has a list of lists for r in range( 0, rows ) : # build the table row by row # need to start a new row for the table row = [] # row is set to be a fresh accumulator print ( "Table= ", table ) # for each column c in the new row, its value is r + c for c in range( 0, columns ) : value = random.randrange( 10, 100 ) row.append( value ) print("Row= ", row) # got a row, let's add it to the table table.append( row ) # Notice how it went through and added the column numbers # as it constructed each row. # let's print the row to see what we got print( row ) print() for r in range(0, rows): total = sum( table[r] ) print ("Sum of ",table[ r ], "is", total ) print() # The sum function takes in a list of numbers so # when you call sum on your list sum( table[r] ) where # table[r] is an index into the lists within the table, it'll # sum each the elements in each interior row in the table and # print that out. # table[r] is each list (row) inside your larger list (table) # where r is a numerical index based on the number of rows print() print( table )