[MUSIC PLAYING]
SPEAKER: Well, hello, one and all, and welcome to our short on while loops.
Now life is full of surprises.
And one surprise for me recently was taking care of this particular plant
here.
Now, it turns out, this plant is pretty picky about the kind of water it gets,
and when it gets watered.
I'm supposed to water this plant when the soil is dry, but not too dry.
And so I check every day, what the moisture level is before I actually
know, should I water this plant or not?
So I thought I'd maybe write a program to remind myself
when to water this plant, depending on how dry the soil is.
And one thing I've learned is that you can actually
measure the moisture content of soil in percent water out
of all the entire soil you might have.
And soil is considered dry, supposedly, when it reaches a moisture level of 20%,
or a little bit less.
So ideally here, I could write a program to maybe check for me once a day,
let's just say, what the moisture content of this plant's soil is.
And if it's 20 or less, go ahead and alert me
that I should be watering this particular plant.
So here, I have a program called water.pi.
And this program actually uses a function called Sample
that I've written elsewhere, but I'm going to just import it and use it
in this program.
No need to worry about how that's implemented in this case.
The purpose of Sample is really, to just sample
my soil, hypothetically in this case, and maybe return to me, the percent
moisture that it has sampled from the soil on this particular day.
So I program in main here is as follows, I'm going to sample my soil,
store the result in moisture, and then print out the current moisture
percentage, or the water content at least of this particular soil
on this particular day.
So go ahead and run Python of water.pi.
And what do we see?
Well today, the moisture is 28%, so not quite dry enough to water.
But what should I do now?
This program only checks once for me.
I'd love for it to maybe keep checking, keep checking, keep
checking while the soil is still wet.
And I'd love for it to stop when the soil is dry
and alert me that it's time to water this particular plant.
So notice how I said there, I wanted to do something
while some condition is true.
I want to keep checking the soil, sampling while the soil is wet.
So I could use, let's say, a while loop.
A while loop is great when I'm not quite sure how many times I want to loop,
but I do want to loop while some condition is true.
Why don't I go ahead here and maybe update
this program to be a little more dynamic and keep
looping while some condition is true.
I'll say maybe while the moisture content, moisture in this case,
is greater than 20.
That is, the soil is still wet, I want to do the following.
I want to sample again.
Maybe it's another day, I'll sample the soil again,
and I'll then report on the actual moisture content, the water
content of this soil here.
So now, what we've done is updated our program.
And from top to bottom, it reads like this.
I'll first sample the soil, perhaps, immediately after I water it, let's say.
I'll start running this program.
It will then report to me the moisture in that soil.
And I'll then, let's say while the moisture is greater than 20,
while the soil is still wet, I will sample,
let's say, once per day, the moisture, and then report to myself
the moisture content as it currently stands.
Now this while loop, the code is indented inside of it, again,
will keep running while this Boolean expression is true.
But once it's no longer true, we'll actually exit out of this loop,
and in this case, end our program.
But before we do, maybe I'll do this.
I'll say, time to water.
Because I know that if moisture is not greater than 20, well, it's equal to 20,
or less.
So I'll go ahead and say it's time to water.
Your soil is getting too dry.
So why don't I go ahead and maybe try this program now.
I'll run Python of water.pi, and I'll see
that we could maybe, just for the sake of the magic
here, assume this happens once a day.
So the first day, 34% moisture.
Next day, 31%, then 27, 23, 22, and 18.
Now 18 is definitely not greater than 20.
So we would then exit our loop and say, time to water.
But as long as moisture was greater than 20, we were, as you notice,
continuing to loop, continuing to loop as we went through.
Now let's just try this again one more time.
And we'll see that seem to take a little bit more, a little bit more time here.
I'm going to try this again.
That seemed to take only three days.
So really, soil moisture content can vary based
on the weather, the sun, et cetera.
And it might really be somewhat random how dry or wet
my soil becomes over time.
Now here, the magic of the while loop is that no matter when
this happens, whether it's four days in, two days in, five days in,
it's always going to end when this condition is
no longer true, in this case.
And to make this point even more clearly, let's go ahead
and add in a variable called Days.
And we'll say the day I first start watering is days equal to 0.
And then every time I iterate, I'll add 1 to Days.
So going from 0 to 1, then 1 to 2.
Every day, I sample my soil again and again,
and I could actually make it clear what day it is.
I'll say day, days, and day, days here.
And now, If I run my program again, and make this just a little bit bigger
for ourselves here, I should hopefully see that this time,
it took five days indexing from 0.
This time, it took six days indexing from 0.
And this time, it only took three days.
So again, while loop, the perfect choice when
you want to keep looping while some condition is true,
but you're not exactly sure how long you're
going to loop at the end of the day.
Good for things like randomness, good for things like this,
where you have a plant, you're not sure when to water.
This was our short on while loops.
We'll see you next time.