1 00:00:00,000 --> 00:00:03,952 [MUSIC PLAYING] 2 00:00:03,952 --> 00:00:05,440 3 00:00:05,440 --> 00:00:09,540 SPEAKER: Well, hello, one and all, and welcome to our short on while loops. 4 00:00:09,540 --> 00:00:11,030 Now life is full of surprises. 5 00:00:11,030 --> 00:00:14,530 And one surprise for me recently was taking care of this particular plant 6 00:00:14,530 --> 00:00:15,350 here. 7 00:00:15,350 --> 00:00:18,950 Now, it turns out, this plant is pretty picky about the kind of water it gets, 8 00:00:18,950 --> 00:00:21,430 and when it gets watered. 9 00:00:21,430 --> 00:00:26,720 I'm supposed to water this plant when the soil is dry, but not too dry. 10 00:00:26,720 --> 00:00:30,460 And so I check every day, what the moisture level is before I actually 11 00:00:30,460 --> 00:00:33,680 know, should I water this plant or not? 12 00:00:33,680 --> 00:00:37,180 So I thought I'd maybe write a program to remind myself 13 00:00:37,180 --> 00:00:41,330 when to water this plant, depending on how dry the soil is. 14 00:00:41,330 --> 00:00:43,570 And one thing I've learned is that you can actually 15 00:00:43,570 --> 00:00:48,520 measure the moisture content of soil in percent water out 16 00:00:48,520 --> 00:00:51,200 of all the entire soil you might have. 17 00:00:51,200 --> 00:00:57,520 And soil is considered dry, supposedly, when it reaches a moisture level of 20%, 18 00:00:57,520 --> 00:00:59,450 or a little bit less. 19 00:00:59,450 --> 00:01:04,500 So ideally here, I could write a program to maybe check for me once a day, 20 00:01:04,500 --> 00:01:08,420 let's just say, what the moisture content of this plant's soil is. 21 00:01:08,420 --> 00:01:11,900 And if it's 20 or less, go ahead and alert me 22 00:01:11,900 --> 00:01:17,210 that I should be watering this particular plant. 23 00:01:17,210 --> 00:01:21,440 So here, I have a program called water.pi. 24 00:01:21,440 --> 00:01:25,460 And this program actually uses a function called Sample 25 00:01:25,460 --> 00:01:28,880 that I've written elsewhere, but I'm going to just import it and use it 26 00:01:28,880 --> 00:01:29,910 in this program. 27 00:01:29,910 --> 00:01:32,630 No need to worry about how that's implemented in this case. 28 00:01:32,630 --> 00:01:34,880 The purpose of Sample is really, to just sample 29 00:01:34,880 --> 00:01:40,220 my soil, hypothetically in this case, and maybe return to me, the percent 30 00:01:40,220 --> 00:01:44,480 moisture that it has sampled from the soil on this particular day. 31 00:01:44,480 --> 00:01:49,070 So I program in main here is as follows, I'm going to sample my soil, 32 00:01:49,070 --> 00:01:53,300 store the result in moisture, and then print out the current moisture 33 00:01:53,300 --> 00:01:56,780 percentage, or the water content at least of this particular soil 34 00:01:56,780 --> 00:01:58,640 on this particular day. 35 00:01:58,640 --> 00:02:02,160 So go ahead and run Python of water.pi. 36 00:02:02,160 --> 00:02:03,180 And what do we see? 37 00:02:03,180 --> 00:02:08,660 Well today, the moisture is 28%, so not quite dry enough to water. 38 00:02:08,660 --> 00:02:11,220 But what should I do now? 39 00:02:11,220 --> 00:02:13,350 This program only checks once for me. 40 00:02:13,350 --> 00:02:17,210 I'd love for it to maybe keep checking, keep checking, keep 41 00:02:17,210 --> 00:02:21,420 checking while the soil is still wet. 42 00:02:21,420 --> 00:02:25,070 And I'd love for it to stop when the soil is dry 43 00:02:25,070 --> 00:02:28,940 and alert me that it's time to water this particular plant. 44 00:02:28,940 --> 00:02:32,100 So notice how I said there, I wanted to do something 45 00:02:32,100 --> 00:02:34,040 while some condition is true. 46 00:02:34,040 --> 00:02:39,240 I want to keep checking the soil, sampling while the soil is wet. 47 00:02:39,240 --> 00:02:42,050 So I could use, let's say, a while loop. 48 00:02:42,050 --> 00:02:46,310 A while loop is great when I'm not quite sure how many times I want to loop, 49 00:02:46,310 --> 00:02:50,660 but I do want to loop while some condition is true. 50 00:02:50,660 --> 00:02:52,820 Why don't I go ahead here and maybe update 51 00:02:52,820 --> 00:02:56,570 this program to be a little more dynamic and keep 52 00:02:56,570 --> 00:02:59,230 looping while some condition is true. 53 00:02:59,230 --> 00:03:04,040 I'll say maybe while the moisture content, moisture in this case, 54 00:03:04,040 --> 00:03:06,200 is greater than 20. 55 00:03:06,200 --> 00:03:10,470 That is, the soil is still wet, I want to do the following. 56 00:03:10,470 --> 00:03:13,020 I want to sample again. 57 00:03:13,020 --> 00:03:15,710 Maybe it's another day, I'll sample the soil again, 58 00:03:15,710 --> 00:03:19,820 and I'll then report on the actual moisture content, the water 59 00:03:19,820 --> 00:03:22,670 content of this soil here. 60 00:03:22,670 --> 00:03:26,000 So now, what we've done is updated our program. 61 00:03:26,000 --> 00:03:27,830 And from top to bottom, it reads like this. 62 00:03:27,830 --> 00:03:32,000 I'll first sample the soil, perhaps, immediately after I water it, let's say. 63 00:03:32,000 --> 00:03:33,750 I'll start running this program. 64 00:03:33,750 --> 00:03:36,530 It will then report to me the moisture in that soil. 65 00:03:36,530 --> 00:03:40,310 And I'll then, let's say while the moisture is greater than 20, 66 00:03:40,310 --> 00:03:43,080 while the soil is still wet, I will sample, 67 00:03:43,080 --> 00:03:47,930 let's say, once per day, the moisture, and then report to myself 68 00:03:47,930 --> 00:03:50,420 the moisture content as it currently stands. 69 00:03:50,420 --> 00:03:54,650 Now this while loop, the code is indented inside of it, again, 70 00:03:54,650 --> 00:03:58,470 will keep running while this Boolean expression is true. 71 00:03:58,470 --> 00:04:02,330 But once it's no longer true, we'll actually exit out of this loop, 72 00:04:02,330 --> 00:04:04,800 and in this case, end our program. 73 00:04:04,800 --> 00:04:07,020 But before we do, maybe I'll do this. 74 00:04:07,020 --> 00:04:09,170 I'll say, time to water. 75 00:04:09,170 --> 00:04:13,640 Because I know that if moisture is not greater than 20, well, it's equal to 20, 76 00:04:13,640 --> 00:04:14,277 or less. 77 00:04:14,277 --> 00:04:16,110 So I'll go ahead and say it's time to water. 78 00:04:16,110 --> 00:04:19,760 Your soil is getting too dry. 79 00:04:19,760 --> 00:04:23,930 So why don't I go ahead and maybe try this program now. 80 00:04:23,930 --> 00:04:27,710 I'll run Python of water.pi, and I'll see 81 00:04:27,710 --> 00:04:30,230 that we could maybe, just for the sake of the magic 82 00:04:30,230 --> 00:04:31,920 here, assume this happens once a day. 83 00:04:31,920 --> 00:04:34,830 So the first day, 34% moisture. 84 00:04:34,830 --> 00:04:40,950 Next day, 31%, then 27, 23, 22, and 18. 85 00:04:40,950 --> 00:04:44,610 Now 18 is definitely not greater than 20. 86 00:04:44,610 --> 00:04:47,850 So we would then exit our loop and say, time to water. 87 00:04:47,850 --> 00:04:52,820 But as long as moisture was greater than 20, we were, as you notice, 88 00:04:52,820 --> 00:04:56,270 continuing to loop, continuing to loop as we went through. 89 00:04:56,270 --> 00:04:59,240 Now let's just try this again one more time. 90 00:04:59,240 --> 00:05:03,970 And we'll see that seem to take a little bit more, a little bit more time here. 91 00:05:03,970 --> 00:05:05,940 I'm going to try this again. 92 00:05:05,940 --> 00:05:08,800 That seemed to take only three days. 93 00:05:08,800 --> 00:05:12,090 So really, soil moisture content can vary based 94 00:05:12,090 --> 00:05:14,470 on the weather, the sun, et cetera. 95 00:05:14,470 --> 00:05:18,120 And it might really be somewhat random how dry or wet 96 00:05:18,120 --> 00:05:21,060 my soil becomes over time. 97 00:05:21,060 --> 00:05:25,650 Now here, the magic of the while loop is that no matter when 98 00:05:25,650 --> 00:05:29,910 this happens, whether it's four days in, two days in, five days in, 99 00:05:29,910 --> 00:05:32,910 it's always going to end when this condition is 100 00:05:32,910 --> 00:05:35,250 no longer true, in this case. 101 00:05:35,250 --> 00:05:37,980 And to make this point even more clearly, let's go ahead 102 00:05:37,980 --> 00:05:40,330 and add in a variable called Days. 103 00:05:40,330 --> 00:05:44,410 And we'll say the day I first start watering is days equal to 0. 104 00:05:44,410 --> 00:05:49,180 And then every time I iterate, I'll add 1 to Days. 105 00:05:49,180 --> 00:05:51,850 So going from 0 to 1, then 1 to 2. 106 00:05:51,850 --> 00:05:54,090 Every day, I sample my soil again and again, 107 00:05:54,090 --> 00:05:58,040 and I could actually make it clear what day it is. 108 00:05:58,040 --> 00:06:02,520 I'll say day, days, and day, days here. 109 00:06:02,520 --> 00:06:07,360 And now, If I run my program again, and make this just a little bit bigger 110 00:06:07,360 --> 00:06:11,380 for ourselves here, I should hopefully see that this time, 111 00:06:11,380 --> 00:06:14,920 it took five days indexing from 0. 112 00:06:14,920 --> 00:06:18,730 This time, it took six days indexing from 0. 113 00:06:18,730 --> 00:06:21,740 And this time, it only took three days. 114 00:06:21,740 --> 00:06:24,490 So again, while loop, the perfect choice when 115 00:06:24,490 --> 00:06:27,940 you want to keep looping while some condition is true, 116 00:06:27,940 --> 00:06:30,100 but you're not exactly sure how long you're 117 00:06:30,100 --> 00:06:32,020 going to loop at the end of the day. 118 00:06:32,020 --> 00:06:35,000 Good for things like randomness, good for things like this, 119 00:06:35,000 --> 00:06:37,570 where you have a plant, you're not sure when to water. 120 00:06:37,570 --> 00:06:39,560 This was our short on while loops. 121 00:06:39,560 --> 00:06:42,120 We'll see you next time. 122 00:06:42,120 --> 00:06:46,000