#---------------------------------------------------------------------- # Illustration of the use of global variables, in the context of # writing out HTML Web files. # # Copyright (C) November 8, 2017 -- Dr. William T. Verts # # File handle MyFile is global to both Write and Main, so a change in # either one is seen by the other. This simplifies the writing of # text to a file, as the file handle (and line break) can now never # be forgotten. However, this is a "back-channel" way to # communicate information between functions that doesn't use the # normal mechanisms of the parameter list or the return statement. # Global variables are to be used cautiously, as detrimental side # effects can occur if programmers are careless. #---------------------------------------------------------------------- def Write (S): global MyFile MyFile.write(S + "\n") return def Main(): global MyFile Filename = pickAFolder() + "Test.html" print Filename #---------------------------------------- # Multiline string, for the top of an # HTML Web file. #---------------------------------------- Prologue = """ My Spiffy Web Page """ #---------------------------------------- # Multiline string, for the bottom of an # HTML Web file. #---------------------------------------- Epilogue = """ """ #---------------------------------------- # Build the Web page by writing out the # prologue, a table of numbers and square # roots from 0 to 1000, and the epilogue. #---------------------------------------- MyFile = open(Filename, "w") Write (Prologue) Write (" ") for I in range(1001): Write (" ") Write (" ") Write (" ") Write (" ") Write ("
" + str(I) + "" + str(math.sqrt(I)) + "
") Write (Epilogue) MyFile.close() return