2014-03-05

posted Mar 5, 2014, 5:26 AM by Samuel Konstantinovich   [ updated Mar 5, 2014, 5:57 AM ]
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:

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

similarly:

to makeHeeey(n):
   ans="H"
   x=0
   while x<n:
       ans=ans+"e"
       x=x+1
   ans=ans+"y!"
   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)
>>>startPage("Bazinga")
"<html>\n<head><title>Bazinga</title></head>\n<body>\n"

>>>startPage("WHOA!")
"<html>\n<head><title>WHOA!</title></head>\n<body>\n"

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

>>>print startPage("Bazinga")
<html>
<head><title>Bazinga</title></head>
<body>


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) →

"""<table>
<tr><td>1</td><td>2</td><td>3</td><td>4</td></tr>
<tr><td>1</td><td>2</td><td>3</td><td>4</td></tr>
</table>"""

makeTable(4,2) →

"""<table>
<tr><td>1</td><td>2</td></tr>
<tr><td>1</td><td>2</td></tr>
<tr><td>1</td><td>2</td></tr>
<tr><td>1</td><td>2</td></tr>
</table>"""

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) →
"""<table>
<tr><td>11</td><td>12</td><td>13</td><td>14</td></tr>
<tr><td>15</td><td>16</td><td>17</td><td>18</td></tr>
</table>"""

makeTable(4,2,133) →
"""<table>
<tr><td>133</td><td>134</td></tr>
<tr><td>135</td><td>136</td></tr>
<tr><td>137</td><td>138</td></tr>
<tr><td>139</td><td>140</td></tr>
</table>"""

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!
Comments