CSC 111: Introduction to Computer Science

Lab 8: Variable Scoping Rules

Thursday, Mar 31, 2011

Part 1: Growing Flowers with Functions

For this part of the lab, work on rewriting any previous program, using functions. You could use my example of HW4. It would probably be more fun to convert any of your earlier programs - unless you have looked at them enough.

Whichever program you choose to convert, first write out the main function, identifying the new functions you will create along with the input and output required by each new function. Then, step by step, convert the program to one that uses functions, and so has a very short and clean main() function, that simply calls the other functions.

You can leave this part of the lab unfinished in order to have about 20 or 30 minutes remaining to work on part 3. Part 3 of the lab will lead into the homework this week, and you want to be sure to have the basics down before leaving lab.

Part 2: Creating an HTML page

For this part of the lab, you will begin writing a Python program that will create an HTML file. This file can be viewed in a web browser and will be the grand beginning of your own web page.

To begin, try running the following program. Run the program, look at the .html file created both in a text editor, and also in a web browser.

  1. Where does the text "A Simple Page" show up in the web browser?
  2. How are comments identified in HTML?
  3. What are the triple quotes doing?
  4. Change things in this file and see how they appear as you view your output file, both in the text editor and in the web browser.

Start of HTML code to copy and paste

def makeHTML():
    file = open("CSC111_Lab8.html", 'w')
    file.write("""\n<html>\n<head><title>A Simple Page</title>                  
                                                                                
</head>                                                                         
<body>                                                                          
<h1>This is a first level heading font</h1>                                     
<p>And here is some text</p>                                                   
                                                                                
<!--This is an HTML comment, which will not be displayed in the browser-->      
                                                                                
<p>Here is some text after the comment.</p>                                     
                                                                                
</body>                                                                         
</html>""")

    file.close()

def main():
    makeHTML()

main()

... end of HTML code to copy and paste

Some basic HTML items are:

Note that there are great resources online for writing HTML. Search for more information on any of the the items introduced below. You can also right-click on other webpages, and select "view source" to see what others are doing.
	•  basic tags:  <html> <head> <body> (along with the closing tags)
	•  white space and new lines:  <p>    </p>  <br />
	•  lists:  <ul>  <ol>  <li> -- unordered list, ordered list, list item
	•  pictures:  <image height = "100"  src = "...jpg"/>
	•  Anchor tag:  <a href = "someplace.html"> link text </a>  ( "someplace.com" etc.)
	•  tables:  <table> <tr> <td>
	•  formatting with color -- RGB in Hex
	•  font:  headings, italics, bold:   <h1>   <i>   <b>