#!/usr/bin/python3
#-------------------------------------------------------------------------------
# Produce a multiplication table 
#-------------------------------------------------------------------------------
print("Content-Type: text/html \n")

print("<h1>Multiplication Tables</h1> \n")


#======= A simple nested loop ======================================================================

print("<h3>Simple nested 'for' loops</h3> \n")

for row in range(1,11):                     #loop through 10 rows
    for col in range (1,11):                #loop through 10 columns
        print(row * col, ' ', end='')       #row times column 
    print()        
print()        


#====== A nested loop with headers ================================================================

print("<h3>Nested 'for' loops with headers</h3> \n")

max = 10                                    #Set max to any number desired

print ('X |', end='')
for heading in range(1,max+1):              #loop through up to max times
    print('\t', heading, end='')
print("\t <br>")         

#print("--|-------------------------------------------------------------------------------- \t <br>")        

print ('--|', end='')
for heading in range(1,max+1):              #loop through up to max times
    print('--------', end='')
print("\t <br>")         

for row in range(1,max+1):                  #loop through up to max times
    print(row, '| \t', end='')
    col = 1
    while col <= max:                       #loop through up to max times
        print(row*col, '\t ', end='')       #row times column 
        col += 1                            #increment col 
    print("<br>")        
print()

    
#===== Placing the output in HTML table ==============================================================

print("<h3>Placing the output in an HTML table</h3> \n")

print("<table border='1'>")

for row in range(1,max+1):                          #loop through max rows
    print("<tr>", end='')       
    col = 1
    while col <= max :                              #loop through max columns
        print("<td>", row * col, "</td>", end='')   #row times column   
        col += 1                                    #increment col 
    print("</tr>")
print("</table> \n")         
             

#==== Adding row and column headers ================================================================

print("<h3>Adding row and column headers </h3> \n")

print("<table border='1'>")
print("<tr bgcolor='cyan'>")       
print("<th width='25'> X </th>")      

for colHead in range(1,max+1):                           #loop max times
    print("<th width='25'>", colHead , "</th>", end='')  #print(column headers

print("</tr>")       

for row in range(1,max+1):                               #loop through max rows
    print("<tr>", end='')       
    print("<th bgcolor='cyan'>", row , "</th>", end='')  #print(row header 
    col = 1;
    while col <= max :                                   #loop through max columns
        print("<td align='right'>", row*col , "</td>", end='') 
        col += 1
    print("</tr>")

print("</table> \n")         


#----- Reprinting with row and column headers (yet another way) ----------------------

print("<h3>Adding row and column headers - yet another way </h3> \n")

print("<table border='1'>")

for row in range(0,max+1):                      #loop through max rows
    print("<tr>", end='')       
    col = 0;
    while col <= max :                          #loop through max columns
        if (row == 0 and col == 0):
            print("<th bgcolor='tan'> X </th>", end='')
        if (row == 0 and col >  0):
            print("<th bgcolor='tan' width='25'>", col, "</th>", end='') 
        if (row >  0 and col == 0):
            print("<th bgcolor='tan' width='25'>", row, "</th>", end='') 
        if (row >  0 and col >  0):
            print("<td align='right'>", row*col , "</td>", end='') 
        col += 1
    print("</tr>")
print("</table> \n")         




#=== link to see the python code =================================================
import os, sys
sys.path.insert(0,'/home/s/sultans/web/python/demo')
import zCode                          #import func to display the Python code
filename = os.path.abspath(__file__)  #get absolute file name 
zCode.display(filename)               #call it
#=================================================================================