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

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


#====== a function to create a multiplication table (not in html table) ============================
def mult(rows, cols):

    print('<br>Multiplication table', rows, 'by', cols, '\n<br><br>')

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

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

    for row in range(1,rows+1):                 #loop through up to max rows 
        print(f'{row:2d} |  ', end='')
        col = 1
        while col <= cols:                      #loop through up to max cols
            print(F'{row*col:4d}', end='')      #row times column 
            col += 1                            #increment col 
        print(" <br>")        
    print()

#====== a function to create a multiplication table (in html table) ================================
def mult2(rows, cols):

    print('<br>Multiplication table', rows, 'by', cols, '\n<br><br>')

    print ('<table border=1 bgcolor=lightyellow>')
    print ('<tr bgcolor=cyan><th> X ', end='')
    for heading in range(1,cols+1):             #loop through up to max cols 
        print('<td>', heading, end='')
    print("</tr>")         

    for row in range(1,rows+1):                 #loop through up to max rows 
        print('<tr><td bgcolor=cyan>', row, end='')
        col = 1
        while col <= cols:                      #loop through up to max cols
            print('<td>', row*col, end='')      #row times column 
            col += 1                            #increment col 
        print('</tr>')        
    print('</table>')

#===================================================================================================
import random

mult(10,10)                                     #call multiplication function
mult2(10,10)                                    #call multiplication function

rows = random.randint(1,10)                     #generate random number 1 thru 10  
cols = random.randint(1,10)  
mult2(rows,cols)    
    




#=== 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
#=================================================================================