Lecture 15 + (LAB05)

posted Feb 25, 2013, 6:13 AM by Samuel Konstantinovich   [ updated Feb 26, 2013, 5:22 AM ]
Day 17: LAB05

hint: strings are very much like numbers in how you can build them from scratch. Think of making a string like making a sum:

while x<10:
   sum = sum + x
print sum
#this calculates the sum of 0-9 


to makeHeeey(n):
   while x<n:
   return ans
#this is how you build up a string with a start, repeating middle, and end

This lab has 3 parts, each will get yu closer to having a function that returns a webpage!

1. Write a function startPage(title) that takes a string title as a parameter. The function should return the beginning of a webpage up to the <body> tag. (do not close the body tag) The title of the webpage should be the string stored in the title variable.

Here is how you should test your functions, notice the parameter is a string, so it requires quotes.
(the character 
"\n" represents a new line)
"<!doctype HTML>\n<head><title>Bazinga</title></head>\n<body>"

"<!doctype HTML>\n<head><title>WHOA!</title></head>\n<body>"

BUT! if you print it, the "\n" is replaced with a new line, so it formats nicely:

>>>print startPage("Bazinga")
<!doctype HTML>

You probably want to use \n in your rows:

2a. Write a function makeRow(numCols) that returns a string of HTML that is a single row of a table that has numCols consecutive integers starting with the number 1:
makeRow(2) → "<tr><td>1</td><td>2</td></tr>\n"
makeRow(4) → "<tr><td>1</td><td>2</td><td>3</td><td>4</td></tr>\n"

2b. Write a function makeRow(numCols,startValue) that returns a string of HTML that is a single row of a table that has numCols consecutive integers in it, but start the numbers at startValue
makeRow(2,1) → "<tr><td>1</td><td>2</td></tr>\n"
makeRow(2,12) → "<tr><td>12</td><td>13</td></tr>\n"
makeRow(4,99) → "<tr><td>99</td><td>100</td><td>101</td><td>102</td></tr>\n"

3a. Write a function makeTable(Rows,Cols) that returns a string of HTML that is a single table  with Rows number of rows, and Cols number of elements per row. You REALLY should use makeRow for this.

makeTable(2,4) →


makeTable(4,2) →


3b. Change the function to have each row continue counting where the previous row left off and has a starting value:
makeTable(2,4,11) →

makeTable(4,2,133) →

4. Now put it all together, make a function makePage() that returns a complete website string. You can print it out and paste it into an HTML file to check if it works.

Make a webpage called lab5.html in your public HTML directory. This should contain a table with 14 rows, and 16 numbers per row starting with the number 32. (the table should have the numbers 32-255)

If your HTML is not formatted well, you should adjust your python program to produce clean code with proper tabs, spaces, and new lines!.