More Variables (Video Version)
-
0:02 - 0:04So, we already made
this super cool variable -
0:04 - 0:06for Winston's eye size,
which has been really helpful -
0:06 - 0:09because it saves us a lot of typing
when we want to change -
0:09 - 0:10the size of Winston's eyes.
-
0:10 - 0:13Remember: before, we had to
type the same number four times -
0:13 - 0:16whenever we wanted
to change the size of his eyes. -
0:16 - 0:18But, can we use variables
to replace numbers -
0:18 - 0:19when they're not all the same?
-
0:19 - 0:22Like, what if I wanted to
move Winston over a little bit? -
0:22 - 0:24I could change
each of these x coordinates, -
0:24 - 0:26but they're not all the same anyways,
-
0:26 - 0:28so I can't just replace them all
with some variable. -
0:28 - 0:29But wait!
-
0:29 - 0:32Once I assign a number to a variable,
I can use that variable -
0:32 - 0:34exactly as if it were that number,
-
0:34 - 0:37which means I can use variables
and math expressions. -
0:37 - 0:39So, how about this:
let's make a variable -
0:39 - 0:41for the x position of Winston.
-
0:41 - 0:43Call it var x.
-
0:43 - 0:45Simple enough.
Okay, now let's give it -
0:45 - 0:48the value of the x coordinate
of Winston's face. -
0:48 - 0:51His face is being drawn
at x coordinate 200. -
0:51 - 0:54So, we're going to say var x gets 200.
-
0:55 - 0:59Now I can replace this number
with my new variable x, -
0:59 - 1:02and when I change the value of x,
it moves Winston's face, -
1:02 - 1:04but not his eyes or his mouth.
-
1:05 - 1:08So, to fix that, we're going to define
-
1:08 - 1:10the position of his eyes and his mouth,
-
1:10 - 1:14relative to the position of his face.
-
1:14 - 1:17The x coordinates
are going to be the centers -
1:17 - 1:20of all the other ellipses,
so his face is being drawn at-- -
1:20 - 1:25that's 200,
and let's do his left eye first. -
1:25 - 1:26So, this ellipse is his left eye,
and we can see -
1:26 - 1:30that it's being drawn at 150.
-
1:30 - 1:33Right here, that is 150,
-
1:33 - 1:38and I know that 150 is 50 less than 200.
-
1:38 - 1:42So, his left eye should be
wherever his face is, minus 50. -
1:42 - 1:48It's going to be x,
which is the position of his face, - 50. -
1:48 - 1:50And now, if I change the value
of this x coordinate, -
1:50 - 1:53his eye moves along with his face.
-
1:53 - 1:55Let's do the other ones.
-
1:55 - 1:57So, his right eye is being drawn at 300,
-
1:57 - 2:01I can see, so this over here is 300.
-
2:01 - 2:06And 300, I know, is 100 more than 200.
-
2:06 - 2:10So, his right eye should be
wherever his face is plus 100. -
2:10 - 2:13That's x + 100.
-
2:13 - 2:14And now we just have his mouth.
-
2:14 - 2:17His mouth is being drawn at 250.
-
2:17 - 2:19That's over here, we've got 250,
-
2:19 - 2:23and 250 is 200 plus 50.
-
2:23 - 2:26So, it's going to be wherever--
ooh, that's a 5-- -
2:26 - 2:31wherever his face is,
x + 50. -
2:31 - 2:33And now--
I'm going to get rid of these scribbles-- -
2:33 - 2:38whenever I change the value of x,
it moves his whole face. -
2:38 - 2:40Yay!
-
2:40 - 2:43Now before you go, you should know
a couple more things about variables. -
2:43 - 2:46Once you create a variable using var x,
-
2:46 - 2:48you can assign a value to it
as many times as you want. -
2:48 - 2:52So, after giving x the value of 198,
-
2:52 - 2:55I could say x gets 300,
-
2:55 - 2:59or x gets 150.
-
2:59 - 3:01And from then on,
the computer would treat x -
3:01 - 3:03as if it were
whatever we last assigned it. -
3:03 - 3:05So, in this case, 150.
-
3:05 - 3:08But, you only need to type var
once per variable. -
3:08 - 3:13If, over here, I had typed
var x gets 150 instead, -
3:13 - 3:16that would have made
a brand new variable named x, -
3:16 - 3:18completely destroying
the old variable x in the process, -
3:18 - 3:22so all these variables
are gone, in the dumpster. -
3:22 - 3:25And then, afterwards, the computer
would only use the new variable. -
3:25 - 3:28Sometimes you can do this kind of thing
without horrible things happening to you, -
3:28 - 3:30but I wouldn't advise it.
-
3:30 - 3:33For now, I'm just going to do it once:
var x gets 150. -
3:33 - 3:37And now, conveniently,
I can move all of Winston off the screen, -
3:37 - 3:40so I can talk about my next point.
-
3:40 - 3:45Whenever you have
a variable equals something, -
3:45 - 3:47that variable is in a happy, safe place.
-
3:47 - 3:51In fact, to the left of the equal sign
is the only place -
3:51 - 3:54where a variable
can truly feel like itself, -
3:54 - 3:56because that is the only place
where the computer -
3:56 - 3:57will consider it to be a variable.
-
3:57 - 4:00Anywhere else, the computer
just pretends it's a value. -
4:00 - 4:02So even if we do something like this:
-
4:02 - 4:06var x gets 10
-
4:06 - 4:11and then x gets x.
-
4:11 - 4:12Woo, confusing.
-
4:12 - 4:15Well, over on the left,
we've got x, -
4:15 - 4:17which is a free-thinking,
independent variable; -
4:17 - 4:20one that can grow, can change!
-
4:20 - 4:25But, on the right, it's just,
well it's just 10. -
4:25 - 4:27So sad!
But that means -
4:27 - 4:29we can do really useful stuff like this:
-
4:29 - 4:34I can say x gets x + 1
-
4:34 - 4:37So, on the left, we've got our variable
in a happy, safe place, -
4:37 - 4:40and on the right, we have this variable,
-
4:40 - 4:43which we know is basically just a number,
-
4:43 - 4:44so it's just 10.
-
4:44 - 4:47So, we can mentally replace
this with x gets 10 plus 1, -
4:47 - 4:51Which just means x gets 11.
-
4:51 - 4:54So, this whole line is basically
just saying x gets 11, -
4:54 - 4:58so from now on the value of x is 11.
-
4:58 - 5:01Let's see what happens if we
bring another variable into the equation. -
5:01 - 5:06I'm going to do var x gets 10,
-
5:06 - 5:10var y gets 20,
-
5:10 - 5:12x gets y --
-
5:12 - 5:14oh, my god--
-
5:14 - 5:17and y gets 7.
-
5:17 - 5:19Okay, and what I want to know is,
-
5:19 - 5:22what is the value of x?
-
5:22 - 5:24Let's figure this out.
-
5:24 - 5:27So, at this line here,
we're making a new variable x, -
5:27 - 5:29and assigning it the value of 10.
-
5:29 - 5:32So the value of x is 10.
-
5:32 - 5:34At this line, we're making
a new variable y, -
5:34 - 5:36and giving it the value of 20.
-
5:36 - 5:37We don't do anything to x,
-
5:37 - 5:39so the value of x is still 10.
-
5:39 - 5:42Over here, we're saying x gets y.
-
5:42 - 5:44Well, that's pretty confusing,
but if you think about it, -
5:44 - 5:46we know that only one of these variables
-
5:46 - 5:48is in the happy, safe place,
-
5:48 - 5:50and the other variable is being treated
-
5:50 - 5:54exactly as if it were a number.
-
5:54 - 5:56So over here, we can mentally replace
-
5:56 - 5:58this y with its value, which is 20,
-
5:58 - 6:02and we know that what this line
really means is x gets 20. -
6:02 - 6:05So, the value of x here is 20.
-
6:05 - 6:09And then, over here, we've got y
in the happy safe place this time, -
6:09 - 6:10but it's not really that confusing,
-
6:10 - 6:13because we've just got a number
on the other side. -
6:13 - 6:17So, here's the big question:
when I change the value of y here, -
6:17 - 6:21does that change the value of x up here?
-
6:21 - 6:26And the answer:
No! No, no, no, no, no! -
6:26 - 6:29You've got to remember, up here,
-
6:29 - 6:32y is just being treated like a number.
-
6:32 - 6:35The computer's going to completely ignore
that it's a variable, -
6:35 - 6:36forget the fact that it can change,
-
6:36 - 6:39and it's just going to say,
-
6:39 - 6:42"Oh, y? What you really mean is 20."
-
6:42 - 6:45So right here,
we're just assigning a number to x, -
6:45 - 6:47we're not assigning a variable.
-
6:47 - 6:50We're assigning the value of y,
which is 20, -
6:50 - 6:54and that means that
the value of x stays 20, -
6:54 - 6:58even after we change the value
of this y variable from the last line. -
7:00 - 7:02Phew! Capisce?
-
7:02 - 7:05Well, now you guys are
total experts on variables. -
7:05 - 7:07Congratulations!
- Title:
- More Variables (Video Version)
- Description:
-
This is just a screen grab of our interactive coding talk-through, prepared to make captioning and translation easier. It is better to watch our talk-throughs here:
https://www.khanacademy.org/cs/programming/ - Video Language:
- English
- Duration:
- 07:10
April Porter edited English subtitles for More Variables (Video Version) | ||
April Porter edited English subtitles for More Variables (Video Version) | ||
April Porter edited English subtitles for More Variables (Video Version) | ||
victoria.thatte edited English subtitles for More Variables (Video Version) | ||
victoria.thatte edited English subtitles for More Variables (Video Version) | ||
victoria.thatte edited English subtitles for More Variables (Video Version) | ||
Uni Mikkelsen edited English subtitles for More Variables (Video Version) |