home—info—lects—labs—exams—hws
—D2L—breeze (snow day)—tutor/PIs
lab08-variable-practice
variables
practice
Review
- Variables are named boxes that hold a value
- The statement salary = 45000
creates a variable named “salary”
and stores an initial value (45000) in it.
- “=” is pronounced “gets”.
Example:
Write a series of expressions to calculate what character corresponds
to 'a' shifted backwards by 32.
Use variables to store intermediate results
(rather than one big expression chr( ord('a')-32 )).
Sample solution:
startLetter = 'a'
startCode = ord(startLetter)
shiftedCode = startCode - 32
shiftedLetter = chr(shiftedCode)
shiftedLetter
|
Self-practice:
Type in and run the following (filling in blanks as appropriate):
-
Set a variable n to hold 15:
-
Set a variable m to hold 6*4:
-
Set a variable p to hold m+2*n:
-
Set a variable gpa to hold 3.2:
-
Set a variable totalCreditHours to hold three times the gpa:
-
Suppose a student has goal: to reach a target-gpa that is 0.2 higher than whatever the
variable gpa currently holds.
Create and initialize a variable to do this; come up with a good name for your variable.
(Don't use the name gpa, because that's already used, and besides your
variable doesn't hold their gpa -- it holds their target gpa!)
- Finally:
In the above, if a student's gpa were already 3.9 it would be an unreasonable goal to
have their gpa increased by 0.2!
Instead, make a variable to hold a realistic-target gpa:
that's the smaller (min) of:
the existing gpa + 0.2 and 4.0.
-
What does the following compute?
story = "Once upon a time, there was a frog, who lived happily ever after."
startOfMiddle = int( 1.0/3.0 * len(story) )
endOfMiddle = int( 2.0/3.0 * len(story) )
middle = substring( story, startOfMiddle, endOfMiddle )
|
TODO:
For each of the following,
have a series of python statements which call a single function
at a time
and store the result into a new variable,
and the following statement will use that variable (and one more function call).
-
-
Up in the editor pane,
make a variable “schoolNameLen”
which holds the length of
“Virginia Polytechnic Institute and State University”.
-
Make another variable “nicknameLen”
to hold the length of “Tech”.
-
Now, down in the interaction pane (after clicking Load),
compute the ratio of the long-length to the short-length
— in other words, how much compression is achieved!
Remember to do floating-point division, not integer division!
-
We'll expand on the previous excercise:
-
start by storing the shool's name (not the length of the name) and nickname
into variables, too.
-
Move these newly-created variable up above where you stored the name's length from before.
Then, change how you define schoolNameLen, so that it refers to the contents
of shoolName, and does not repeat the entire school's name.
The goal is that if Tech changes their name to
“Virginia Poyltechnic Institute and State University of the World!”,
then will only need to change one thing so that
both your variables are correct.
Or, if we are suddenly interested in the compression achieved between
“Radford University” and “RU”,
then we only need to change the name and nickname
without changing anything about how we compute the (nick)name's length.
-
Finally, also give a (meaningful) name to the final result (a total of five variables).
Discuss w/ your partner:
We have seen doing this with 0 variables,
with 2 variables and with 5 or 5.
Which do you prefer, and why?
-
In the editor-pane (top half),
write an equivalent series of expressions to show( makePicture( pickAFile() ) ):
- Make a variable to hold the result of calling pickAFile().
(Btw: what type are you storing in that variable?)
- Make a second variable which holds the result of calling the next
function above needed, in the expression.
(Btw: what type are you storing in that variable?)
- Finally, call show on whatever you just stored in the variable
from the previous step.
Note that show does not actually return a variable —
it does something (draw pixels on the screen), but doesn't
return anything.1
(Note that when you press Load, it runs everything in your file —
including your one call to pickAFile(),
and your one call to show.
After loading, you can inspect the contents of your first variable,
and see the name of the file you picked.)
Note that if we first want to do one thing, and use that result in the next step, and so-on,
we can either
compose functions,
or we can
make up name for intermediate results and store those results in those variables.
If you finish the above,
you can start working on the next problem.
1
We say that this function has a “side effect”,
since calling it multiple times gives you different stuff (multiple windows),
unlike calling len multiple times which
.
↩
home—info—lects—labs—exams—hws
—D2L—breeze (snow day)—tutor/PIs