Introduction to Bit Transcript
[00:00:00]
INSTRUCTOR: Hello, today we’re going to talk about bit. Bit is a visual tool
that we’re going to use to learn about programming and we hope it’ll be
useful to you as we get started. So the first thing I want to show you is that
we have our bit world.
Start visual description. A grid is shown on screen of World 1: New World(5,3) with a light blue triangle representing a bit.
[00:00:15]
And this is bit. This is this little triangle here, this bit. We use that, notice
which way it’s pointing. It’s pointing to the right. And as we go through bit, we
have several commands that we can use with them.
[00:00:32]
We can tell them to go forward. We can have them turn left or right. And we
can have them paint square that he’s on. So if you were to try to get to this
square, I want you to think about how you would go about doing that.
[00:00:48]
So we’ve got bit starting here, and we want to get him to this square over
here. And we want him to be pointing up when he’s done. Think about if we
have move as one of our commands for bit. Think about if we have paint a
color, and then we also have turn left and turn right.
[00:01:11]
So what would you do? Pause the video and figure that out. OK, so what you
would do is tell bit to move. So he would go from this one to this one, and
then move again, and then move again, and move again.
[00:01:34]
And then finally, we want bit to turn to the left. And so we would give him a
command to turn to the left, and then we would move again. And then we
would paint the square that he’s on green. So that’s just the basic idea that
we’ll go into more details as we go.
[00:01:54]
But I wanted you to see how this might work before we get started. Bit is
packaged, which is code that another person has written and we install that
using a pip command. So we’ll say conda run -n, it’s important to copy this
exactly, cs110 pip install byubit and once we’ve installed this package we can
use it in a script or program.
Start visual description. Slide titled Packages. Text reads:
We can install code that other people wrote (called a “package”) using the pip
command in our terminal
We’ll use pip install byubit:
Conda run –n cs110 pip install byubit
Once we’ve installed a package, we can use it in a script.
End visual description.
[00:02:20]
So the way we do that is every program that we’re going to write with bit
we’re going to have from byubit import bit and this means that what we’ve
installed is a thing called bit and we want to use it in our program.
Start visual description. Slide reads:
From byubit import bit
This means that byubit (what we pip installed) has a thing called Bit, and we
want to use it.
End visual description.
[00:02:42]
We will define a function that will tell bit what to do. So here we have def,
that means it’s a function and we’re going to call move around bit and so this
is what we’re going to do.
Start visual description. Slide reads:
def move_around(bit):
Here we define a function that will tell Bit what to do.
In this case, we named the function parameter bit, but we could have named
it robot or Sally or cosmo.
def move_around(bit):
cosmo.move()
We’ll talk more about functions in a few lectures.
End visual description.
[00:02:54]
In this case we’ve named it the function parameter bit right here. You could
also name that Cosmo and either way is fine. We’ll talk more about functions
in the future but that’s just enough to get you started.
[00:03:11]
Bit, empty world, notice the app symbol here. Five and three means that
we’ve created a world that is five squares across and three squares long. And
that’s our empty world. When bit starts, he always starts in the bottom left
corner and it’s always facing right.
Start visual description. Slide reads:
Bit.empty_world(5,3)
This means that we want Bit (the thing we imported from byubit) to run our
function.
In this case, Bit will create an empty world that is 5 spaces wide and 3 spaces
tall.
When Bit starts in an empty world, it always starts in the bottom-left corner
facing right.
End visual description.
[00:03:34]
So keep that in mind as you write your programs. These lines are instructions
that we have for bit. We have bit.move and notice the parentheses here. If
you don’t put these in here, bit won’t do anything.
Start visual description. Slide reads:
bit.move()
bit.paint (“red”)
These lines are the instructions we have for bit.
bit.move is a function.
Including () at the end means we want to call that function. Calling bit.move()
makes the bit move one space in the direction it is facing.
End visual description.
[00:03:48]
And we have bit.paint and red. So we could also use red. We could also use
blue and green. So when we use these parentheses, that means we’re calling
the function, the same with this one. And bit.move just makes it move one
space to, or in the direction and space.
[00:04:12]
Bit.move just makes it move one space in the direction and space. So bit.paint
is a function. We’ll talk more about those next time. Including red at the end
means we want to call that function with red as a parameter.
[00:04:30]
We will paint the current space specified color. And like I said, we have red,
blue, and green. Notice you have to have those in quotes. If you don’t have
those in quotes, it’ll give you an error. Okay, another thing, indentation
matters.
Start visual description. Slide reads:
bit.move()
bit.paint (“red”)
bit.paint is a function.
Including (“red”) at the end means we want to call that
function with “red” as a parameter.
Calling bit.paint(“red”) will paint the current space the specified color. Valid
colors are “red”, “blue”, and “green”. (Notice the quotes “)
End visual description.
[00:04:47]
So the lines inside this, this is a function. The lines in that side, that function
are indented at about four spaces. And this matters. This, anything that
belongs in that function needs to be indented.
Start visual description. Slide titled Indentation Matters! Slide reads:
def move around (bit):
bit.move()
bit.move()
bit.paint(“red”)
You’ll notice that the lines inside the function are indented 4 spaces. This
matters.
Be sure to indent the code that belongs to a function.
End visual description.
[00:05:02]
Each of our programs will have this part right here. We’ll have if name equals,
and notice these are underscores here, equals main. This is telling bit, or this
is telling our program what to run. And so as its first thing, this is kind of
complicated.
Start visual description. Slide reads:
@Bit.empty_world (5, 3)
def move around (bit):
if name
main
move around (Bit.new bit)
End visual description.
[00:05:20]
We’ll get more into it later. So just copy and paste these lines inside your
code, and we will get it to what that means more later. Okay, let’s get ready to
try a few things with it. Okay, so you can find these files on the course
website.
[00:05:38]
But what I’m going to do now is just go through a couple of examples so you
can see how it works. So we have a program here and we’ve called this
program move around. Notice we’ve named move around here and inside our
program we do different things.
[00:05:55]
So, let’s think about what this program might be doing before we actually run
it. We have a bit empty world 5, 3, and that means that our world is five
across and three up and down. So you can imagine that world, it would be
great if you wrote this down you hand wrote it, it would be great if you drew
this out by hand.
[00:06:21]
So remember that our bit starts in the bottom left hand corner, and we tell it
to move. Okay, what we’re going to do now is we’re going to go through a
program. We’re going to see how it works and give it a try and hopefully you
can predict what it’s going to do as well.
[00:06:47]
So, let’s get started. Okay, so what we have here is, that I would finish my
program but we’re going to go back to first side to just click on that first
button. And then I look at my code. So what I’m going to do now is I noticed
that the first thing that bit is going to do is move.
Start visual description. Screen has the following code:
from byubit import Bit
@Bit.empty_world (5, 3)
def move_around (bit):
bit.move()
bit.left()
bit.move()
bit.paint(“green”)
bit.right() bit.move()
bit.paint(“red”)
bit.right() bit.move()
bit.paint(“blue”)
if name == ’main’: move_around(Bit.new_bit)
The instructor runs code line by line to see how it affects the grid.
End visual description.
[00:07:10]
So what would it do first? It will move forward because it always moves in the
direction it’s pointing. So we click on next to see its next spot. Now, the next
command, you can see that it’s line six is what just ran.
[00:07:27]
So what we’re going to do next is line seven, and the command for that one is
left. So, my bit is going to turn left. Notice it tells us what it’s going to do what
line it’s on. And now we’re going to move again.
[00:07:42]
So, always you think it was going to do. Okay, if you thought it was going to go
up here, that’s right. And now it’s going to paint green. Remember, it only
paints the square it’s on, so we paint that one green.
[00:08:00]
Now the next command is on line 11, and that is, notice the line numbers
over here, bit right. So that means bit will turn right and is pointed in that
direction now. And now we’re going to move again.
[00:08:15]
We’ll move forward the direction bit’s pointed. And now we’re on line 13,
which is paint red. So we’re going to paint that square red. Next, we’re going
to turn bit to the right again. So which direction would it go this way or this
way?
[00:08:32]
Sometimes for me I have to put myself in a bit spot. So I have to turn and
move to exactly where it’s going to be to figure out what direction it’s going
and figure out the puzzle because these are just really puzzles.
[00:08:47]
And turn right is our next step, so it’s going to turn to the right and move
forward and paint blue, and that is the end of our program. You can as you
get going with these it’s important to be able to draw out the world and
figure out what it’s going to do in advance.
[00:09:09]
This will be handy on tests and also as you program because you want to
figure these things out in advance. What I want you to do now is go ahead
and figure out what you would write to program this.
[00:09:25]
So what would you do to get bit to go to here and to paint the square green?
There are different methods of doing it, so go ahead and figure out what that
might look like. Okay, so the first thing we’re going to do is, you could either
go up to this line now, or you can go straight.
[00:09:49]
So I’m going to choose to go straight. So I’m going to move a bit forward,
move again, move again, and move again. And now what’s my next step?
Now I need to turn it to the left. So I’m going to turn a bit to the left.
[00:10:05]
And now I’m going to go forward. And now I need to paint that square green.
So that is how that would work. It’s really important that you draw this out
ahead of time. Okay, so I’ve got a little board here so we can figure out what
this program is going to do.
[00:10:24]
So this arrow is going to represent my bit. So the first thing we do is we do
this line six, which is bit move. So that means I’m going to go to this square
right here.
Start visual description. Screen has the following code:
@Bit.empty_world (5,3)
def make_a_cake(bit):
bit.move()
bit.paint(‘red’)
bit.move()
bit.paint(‘green’)
if name
== ’main‘:
make_a_cake (Bit.new_bit)
End visual description.
[00:10:44]
Now I’m going to paint this square red. Let’s paint that square red. And then
the next thing I’m going to do is move bit again. So now it’s right here. And
then the next command is paint green. So I’m going to paint that square
green.
[00:11:12]
And that’s where our program ends. Okay, let’s look at this program now. We
have bit.move, bit.move, and then bit.paintgreen. So what do you think this
program does? This program would actually just paint this square green.
[00:11:32]
There’s something missing over here that is our problem. And that is these
parentheses right here. Remember, it doesn’t do anything unless we use the
parentheses there, so it won’t call move. So if we have those parentheses in
there, let’s try that and see what happens.
Start visual description. Screen has the following code:
@Bit.empty_world (5, 3) def main(bit):
bit.move
bit.move
bit.paint(“green”)
if name == ’main’: main (Bit.new_bit)
End visual description.
[00:11:53]
So we’ve got bit move. So we’re going to move our bit forward. Okay, and
then bit move again. So we’re going to move to this spot. And then we’re
going to paint that square green. So this one becomes green.
[00:12:13]
So don’t forget those parentheses there. That’s a very important part of this
program. And if you ever have a program that isn’t doing what you expected
it to do, go back and make sure all the parentheses are there.
[00:12:28]
Okay, let’s look at this program for a minute. So this one we have bit move. So
we’re going to do bit move and we’ll move it once to the spot. And then we’re
going to move it again. So now we’re on line seven.
[00:12:43]
And then we’re going to do it again on line eight. And then we’re going to do
it again on line nine. And then we’ve run out of space. So let me show you
what happens here. So let’s run this program and see what happens.
Start visual description. Screen has the following code:
@Bit.empty_world(5, 3)
def go_go_go (bit):
bit.move()
bit.move()
bit.move()
bit.move()
bit.move()
bit.paint(‘green’)
if name ==
’main‘:
go_go_go(Bit.new_bit)
End visual description.
[00:13:03]
We are first moving, and then we move again from line seven. Move again,
line eight. Move again with line nine. And we’ve run out of space. So let’s
check what happens. So it tells us bit tried to move to 5, 0, but that is out of
bounds.
[00:13:23]
So we’ll do that and it ends the program there. So keep that in mind, you have
to stay inside bit’s world. And if it goes outside, it’ll give you an error. Hey,
let’s look at this one. So we have our empty world.
[00:13:41]
And then we call paint stuff, which is great. And then we say bit move, which
is good. And then it paints. But notice it’s missing something. This tells us that
there’s one argument that it needs to happen, zero it’s given.
Start visual description. Screen has the following code:
@Bit.empty_world(5, 3)
def paint_stuff(bit):
bit.move() bit.paint()
if name == ’main’: paint_stuff(Bit.new_bit)
End visual description.
[00:13:58]
So we need to tell it what color we’re going to paint it before we continue
with our program. So the way we fix that is, let’s say blue. And actually, let’s
do it without the book so you can see what that looks like.
[00:14:12]
So if I have blue, notice the red squigglies. And notice that the name blue is
not defined as the error that it gives us. So let’s go ahead and add our quotes.
The red squiggly line goes away because that is telling us that it was not good
before and it’s fine now.
[00:14:34]
And then we’ll run our program again. And this time it works just fine. So
keep in mind we need to make sure to give it a color when we want it to paint
and that color needs to be in quotation marks.
[00:14:51]
One other thing that is good to note is the quotation marks don’t have to be
double quotation marks because you can also have a single quotation marks. I
go back and forth other languages that I’ve used have had only double
quotation marks, so I use those more frequently but you can use single
quotation marks or double quotation marks.
[00:15:14]
As we go on there will be some differences but for right now you can use
either one and it’s just fine. Okay let’s look at this program. What do we think
it’s going to make? So the first thing we notice is that it’s going to paint green
and then it’s going to move and it’s going to paint blue.
Start visual description. Screen has the following code:
@Bit.empty world (5,3) def go(bit):
bit.paint(‘green’)
bit.move()
bit.paint(‘blue’)
if
_name
go (Bit.new_bit)
main :
End visual description.
[00:15:33]
Let’s go ahead and run it. Okay here we go. So we have first we paint it green,
so it should paint that square it’s on. Then we’re going to move forward and
it’s going to paint blue. Okay let’s look at this program and see what it’s going
to do.
[00:15:56]
Notice we have, it’s all ready to go. We’re starting here and we’re going to
paint green, so let’s go the next step. Okay so this is another thing that you
would need to watch out for is we need to make sure that these are
lowercase because it does not recognize capital letters there, so when you’re
writing your programs be sure to use lowercase for your colors.
[00:16:25]
We’ll run that again to show you. Okay so we have first we’re going to paint
the green we’re going to move and then we’re going to paint blue, so don’t
forget to keep those lowercase. And then things will be better.
[00:16:43]
Hey, one more thing that we’re going to talk about today is that you can start
with predefined worlds and we’ll often use these in the programs we’re going
to write. So this one gives us a grassy field which is the initial state.
[00:16:57]
So notice bit is not in its usual spot but when we add a world, it can specify
where we want to land and anything else that we want about it. So this one
has a line of green already. That’s our grassy field.
[00:17:13]
And now I want to paint the sky blue. So the next thing we do is paint one
blue, and that is not enough what would we need to do to finish this
program. So then we’d need to say bit.move and then bit.paint and we want
it to be blue.
[00:17:37]
And it’s good to check your program as you go, so I’m going to go ahead and
do that. Notice I have my paint one blue, then we move and then we paint
another blue. And let’s see, we need to do that four more times.
[00:17:56]
So I’m going to go like this. How many times should I be painting blue? I
should be painting blue five times, because my field is five. And let’s go ahead
and run that. Okay, notice I’ve got, and this world is actually six by four, so it’s
a different size.
Start visual description. Screen has the following code:
@Bit.worlds (“grassy_field”) def make_sky (bit):
bit.paint(‘blue’) bit.move()
bit.paint(‘blue’) bit.move()
bit.paint(‘blue’) bit.move()
bit.paint(‘blue’) bit.move()
bit.paint(‘blue’)
bit.move()
if name == ’main’: make_sky (Bit.new_bit)
End visual description.
[00:18:24]
So when I get to this one, it’s going to compare to its desired results. So it says
bit world does not match expected world. So what do we need to do to
change that? We need to paint one more time. So bit.paint and blue.
[00:18:43]
And notice now that we’ve matched it, it doesn’t have the error and it says its
compare is correct. And for a lot of our programs that we’re going to have you
do, we’ll have you have that. So you can check to see if you’re right.
[00:18:56]
And you’ll be able to work on your code until you get the right program. Okay,
that’s it for today. Play around with bit and go to lab to answer any questions.
And we’ll see you next time.