WEBVTT

00:00:00.880 --> 00:00:02.879
congratulations on completing cleanup

00:00:02.879 --> 00:00:04.400
challenge number two

00:00:04.400 --> 00:00:06.319
we're going to learn one more coding

00:00:06.319 --> 00:00:08.720
concept during our time together today

00:00:08.720 --> 00:00:11.679
if statements you're actually very

00:00:11.679 --> 00:00:13.360
familiar with if statements already

00:00:13.360 --> 00:00:15.200
you use them all the time in your daily

00:00:15.200 --> 00:00:16.640
life for example

00:00:16.640 --> 00:00:18.720
if the password you enter is correct you

00:00:18.720 --> 00:00:20.560
unlock your phone

00:00:20.560 --> 00:00:22.480
going back to the waste chart it might

00:00:22.480 --> 00:00:24.080
use an if statement to keep from running

00:00:24.080 --> 00:00:25.519
into another object

00:00:25.519 --> 00:00:28.720
if there is a wall turn left so in

00:00:28.720 --> 00:00:29.519
programming

00:00:29.519 --> 00:00:31.679
if statements help computers react to

00:00:31.679 --> 00:00:33.680
the world around them

00:00:33.680 --> 00:00:35.360
so when might carol need to react to the

00:00:35.360 --> 00:00:36.719
world around her

00:00:36.719 --> 00:00:38.399
well let's look at the following

00:00:38.399 --> 00:00:40.719
scenario carol picks up litter on her

00:00:40.719 --> 00:00:41.840
street one day

00:00:41.840 --> 00:00:43.840
then the next week she wants to pick up

00:00:43.840 --> 00:00:44.879
litter again

00:00:44.879 --> 00:00:46.719
carol notices that there's lost litter

00:00:46.719 --> 00:00:49.280
her hard work is paying off

00:00:49.280 --> 00:00:51.600
as programmers our goal is to write code

00:00:51.600 --> 00:00:54.160
that can be used in multiple scenarios

00:00:54.160 --> 00:00:55.840
so let's see what happens when we use

00:00:55.840 --> 00:00:59.359
the same code for week 1 and week 2.

00:00:59.359 --> 00:01:02.320
oh no our program throws an error you

00:01:02.320 --> 00:01:04.239
may have already run into this problem

00:01:04.239 --> 00:01:06.159
if we tell carol to pick up a piece of

00:01:06.159 --> 00:01:08.240
trash when there isn't trash there

00:01:08.240 --> 00:01:11.119
we get a bug wouldn't it be great if

00:01:11.119 --> 00:01:12.320
carol could ask

00:01:12.320 --> 00:01:14.400
is there a piece of trash here before

00:01:14.400 --> 00:01:16.560
trying to pick up a piece of trash

00:01:16.560 --> 00:01:18.320
this would enable her to react to

00:01:18.320 --> 00:01:21.280
whether or not there is a piece of trash

00:01:21.280 --> 00:01:23.360
and lucky for us carol can ask that

00:01:23.360 --> 00:01:25.759
question by using an if statement

00:01:25.759 --> 00:01:27.439
this is what an if statement looks like

00:01:27.439 --> 00:01:29.040
in python this

00:01:29.040 --> 00:01:31.840
asks is there a tennis ball or trash

00:01:31.840 --> 00:01:33.439
where carol is standing

00:01:33.439 --> 00:01:36.560
if there is pick it up just like for

00:01:36.560 --> 00:01:37.920
loops and basic commands

00:01:37.920 --> 00:01:39.520
we have to write if statements in a very

00:01:39.520 --> 00:01:41.680
specific way for carol to understand

00:01:41.680 --> 00:01:43.600
them

00:01:43.600 --> 00:01:47.119
if statements start with a lowercase if

00:01:47.119 --> 00:01:49.040
then you use the false present command

00:01:49.040 --> 00:01:51.439
to ask is there a ball present

00:01:51.439 --> 00:01:53.360
note that there is a space before the

00:01:53.360 --> 00:01:55.840
ball's present command

00:01:55.840 --> 00:01:57.920
then you follow this with parentheses

00:01:57.920 --> 00:01:59.840
and a colon

00:01:59.840 --> 00:02:01.759
indented under the if statement are the

00:02:01.759 --> 00:02:03.520
commands that carol executes

00:02:03.520 --> 00:02:06.560
if the answer is yes to put it all

00:02:06.560 --> 00:02:08.479
together this command says

00:02:08.479 --> 00:02:11.280
is there a piece of trash where carol is

00:02:11.280 --> 00:02:12.720
if the answer is yes

00:02:12.720 --> 00:02:15.120
carol executes the take ball command and

00:02:15.120 --> 00:02:17.360
picks up the piece of trash

00:02:17.360 --> 00:02:19.680
if the answer is no carol ignores the

00:02:19.680 --> 00:02:20.640
take ball command

00:02:20.640 --> 00:02:23.520
and continues with the program and those

00:02:23.520 --> 00:02:25.440
are the basics of if statements

00:02:25.440 --> 00:02:26.959
let's go into the editor to see what

00:02:26.959 --> 00:02:29.680
this looks like in carol's world

00:02:29.680 --> 00:02:31.920
here we are in the code editor let's

00:02:31.920 --> 00:02:33.440
take a moment to read our directions for

00:02:33.440 --> 00:02:35.040
this exercise

00:02:35.040 --> 00:02:36.560
carol has been picking up litter on her

00:02:36.560 --> 00:02:38.239
street for the past two weeks

00:02:38.239 --> 00:02:39.519
change the world to see how carol's

00:02:39.519 --> 00:02:40.800
cleanups are impacting the amount of

00:02:40.800 --> 00:02:42.400
litter from week to week

00:02:42.400 --> 00:02:44.400
then write one program that has carol

00:02:44.400 --> 00:02:46.080
clean up the litter regardless of which

00:02:46.080 --> 00:02:46.400
week

00:02:46.400 --> 00:02:49.120
it is great so we can see the difference

00:02:49.120 --> 00:02:50.720
between week two weeks over here in this

00:02:50.720 --> 00:02:52.400
world drop down so if we look at week

00:02:52.400 --> 00:02:53.120
two

00:02:53.120 --> 00:02:54.560
we can see that carol's litter cleanup

00:02:54.560 --> 00:02:56.160
is working although there is still

00:02:56.160 --> 00:02:56.640
litter

00:02:56.640 --> 00:02:59.280
there's definitely less now our job is

00:02:59.280 --> 00:03:01.040
to write a program that works for both

00:03:01.040 --> 00:03:01.599
weeks

00:03:01.599 --> 00:03:02.879
and this is what programming is all

00:03:02.879 --> 00:03:04.959
about writing programs that work in

00:03:04.959 --> 00:03:06.720
multiple situations

00:03:06.720 --> 00:03:08.720
so we want carol to walk down the street

00:03:08.720 --> 00:03:10.080
and if there's a piece of trash

00:03:10.080 --> 00:03:11.840
carol should pick it up we need her to

00:03:11.840 --> 00:03:13.840
do this one two three four five

00:03:13.840 --> 00:03:16.400
six seven eight nine times so i'm

00:03:16.400 --> 00:03:17.280
immediately thinking

00:03:17.280 --> 00:03:20.800
we can use a for loop so 4i in range

00:03:20.800 --> 00:03:23.120
we said it nine times great so we'll

00:03:23.120 --> 00:03:23.920
press enter

00:03:23.920 --> 00:03:25.519
remember that once you have a colon in

00:03:25.519 --> 00:03:27.360
the press enter it automatically indents

00:03:27.360 --> 00:03:28.239
for you

00:03:28.239 --> 00:03:30.080
so in our loop we know we need carol to

00:03:30.080 --> 00:03:31.760
move all the way across the street

00:03:31.760 --> 00:03:33.280
so we know we need her to do the move

00:03:33.280 --> 00:03:35.280
command nine times

00:03:35.280 --> 00:03:37.120
then we need carol to respond to her

00:03:37.120 --> 00:03:38.480
environment by asking

00:03:38.480 --> 00:03:40.239
if there's a piece of trash so we can

00:03:40.239 --> 00:03:41.599
use an if statement for this

00:03:41.599 --> 00:03:43.040
and so remember we also have the quick

00:03:43.040 --> 00:03:44.640
docs here so we can use those if we

00:03:44.640 --> 00:03:46.560
forget the structure of an if statement

00:03:46.560 --> 00:03:49.840
so if ball's present

00:03:49.840 --> 00:03:52.400
colon enter again so now we're indented

00:03:52.400 --> 00:03:53.280
a second time

00:03:53.280 --> 00:03:56.480
we want her to take the ball so really

00:03:56.480 --> 00:03:57.680
quickly let's take a look at the

00:03:57.680 --> 00:03:58.799
indentation here

00:03:58.799 --> 00:04:00.640
because it might look a little funny so

00:04:00.640 --> 00:04:02.319
here notice that our move and if

00:04:02.319 --> 00:04:04.000
statement are indented once

00:04:04.000 --> 00:04:05.519
this shows that these are part of our

00:04:05.519 --> 00:04:07.120
for loop so they will repeat

00:04:07.120 --> 00:04:09.599
nine times then we have the take ball

00:04:09.599 --> 00:04:11.280
command indented again

00:04:11.280 --> 00:04:13.680
as part of our if statement and so let's

00:04:13.680 --> 00:04:15.519
walk through the program to see how the

00:04:15.519 --> 00:04:17.440
entire program works together

00:04:17.440 --> 00:04:19.040
so i'm actually instead of pressing run

00:04:19.040 --> 00:04:20.720
i'm going to step through the program so

00:04:20.720 --> 00:04:22.880
we can see

00:04:22.880 --> 00:04:24.800
so carol is going to move if there's a

00:04:24.800 --> 00:04:26.000
ball present there is

00:04:26.000 --> 00:04:27.280
she's going to execute the take ball

00:04:27.280 --> 00:04:29.360
command i'm going to move again

00:04:29.360 --> 00:04:30.880
check to see if there's a ball present

00:04:30.880 --> 00:04:32.639
there's not a ball present so

00:04:32.639 --> 00:04:34.960
she skips the take ball command move

00:04:34.960 --> 00:04:35.759
again

00:04:35.759 --> 00:04:38.800
is there a ball present yes pick it up

00:04:38.800 --> 00:04:40.800
so we can see if we reset our program

00:04:40.800 --> 00:04:42.479
and let's go ahead and run it

00:04:42.479 --> 00:04:44.800
that carol will go through the for loop

00:04:44.800 --> 00:04:48.240
move and then pick up trash as she goes

00:04:48.240 --> 00:04:49.680
let's see if it does exactly what we

00:04:49.680 --> 00:04:54.720
want it to

00:04:54.720 --> 00:04:56.479
all right it worked for week two let's

00:04:56.479 --> 00:04:58.080
double check with week one it should

00:04:58.080 --> 00:05:00.320
work in week one but just to make sure

00:05:00.320 --> 00:05:03.520
let's reset and then run again and there

00:05:03.520 --> 00:05:04.400
we go

00:05:04.400 --> 00:05:06.160
now we've used for loops and if

00:05:06.160 --> 00:05:07.919
statements to help carol pick up trash

00:05:07.919 --> 00:05:08.880
in her community

00:05:08.880 --> 00:05:10.720
now it's your turn to complete cleanup

00:05:10.720 --> 00:05:12.560
challenge number three and the ultimate

00:05:12.560 --> 00:05:13.440
cleanup challenge

00:05:13.440 --> 00:05:16.000
good luck
