A. Remember, you concatenate strings (combine two or more strings) using the "+" operator. But you cannot concatenate a number with a string, so that the operation (assuming x=45): "Answer is: "+x does not work. You have to convert the number into a string, and then concatenate it. You can do this with the str() function, and so: "Answer is: "+str(x)+' inches' will result in: "Answer is 45 inches".
B. You can use a variable to hold a very long string, and manufacture that long string in pieces. For instance:
s = "first line<br>" s = s + 'second line<br>' s = s + 'third line.'
s is now a long string of HTML code, looking like this:
'first line<br>second line<br>third line.'
You can continue to add more and more strings into s, until, say, s consists of all of the HTML for an entire web page, if you want.
C. We'll cover the while construct on Wednesday, but it's very similar to the while construct in Netlogo -- it's used to repeat a set of statements. For instance, to print out the numbers between 6 and 10:
x = 6 while x <= 10: print x x = x + 1
Exercises:
1. Create the function Max2(a,b), which will be given two numbers and will return the larger of them (either, if they're equal).
2. Create the function Max3(a,b,c) which will be given three numbers and will return the largest, and you MUST use Max2() as a helper function.
3. Create the function ProdSum(fred,harry) which will be
given two numbers and will return the HTML string that will show the product and
sum of the two numbers, with the formatting below:
Example: ProdSum(13, 0.5) will produce the HTML that will show this (the numbers
are colored and in bold-face):
4a. Create the function MultTable(factor, low_int, high_int)
that will be given a number (factor), and a low integer and a higher integer,
and will print a multiplication table like the one in the example below.
This is a case where the function, in fact, does NOT return anything (and no
return statement is needed), but rather prints its results instead.
Example: MultTable(1.5, 2, 6) should print the following lines:
1.5 * 2 = 3.0
1.5 * 3 = 4.5
1.5 * 4 = 6.0
1.5 * 5 = 7.5
1.5 * 6 = 9.0
4b. Create the function MultTableHTML(factor, low_int,
high_int) that will be given a factor, and a low integer and a higher
integer, and will create a multiplication table in HTML what will look like the
one below.
Example: MultTable(45.1, 3, 8) will produce the HTML table showing the
following:
or, if you'd like a challenge (the table is centered in the window, and it has a single, centered title):
Use the idea of creating the HTML string in pieces as shown in the Notes (B) above. TEST! your result by placing the result into a blank HTML page and asking your browser to render it.
Challenge problem: The equation: ax2 +
bx + c = 0 has either 0 or 1 or 2 real roots. Create a function called LargerRoot(a,b,c)
which will be given the three coefficients of the equation, and will return
either the larger of two real roots, or the sole root (if there's only one), or
the string "No roots" when there are no real roots, or the equation is
impossible to solve. Note: each argument can be any number,
including 0, so be careful!
You'll need the sqrt() function, so include at the top of your program the
following line:
from math import *
Examples (and test these cases and others with your function):
LargerRoot(1, -4, -5) returns 5
LargerRoot(2, -4, 2) returns 1
LargerRoot(0, 1, 18.5) returns -18.5
LargerRoot(1, 0 ,1) returns "No roots"
LargerRoot(0, 0, 5) returns "No roots"