While Loops - CS50P Shorts
Skills:
Algorithm Basics80%
Key Takeaways
Demonstrates the use of while loops in Python to solve a real-world problem of reminding when to water a plant based on soil moisture levels
Full Transcript
[Music] well hello one and all and Welcome to our short on wild 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 PL 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 kind of check every day what the moisture level is before I should actually know should I water this plant or not so I thought I could WR 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 uh supposedly when it reaches a moisture level of 20% or a little bit less so I ideally here I could write a program to maybe check for me once a day let's just say uh 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 time be I should be watering this particular plant so here I have a program called water. py 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 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 uh return to me the percent moisture that it has sampled from the soil on this particular day so my 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 waterp 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 uh wet and I'd want love for it to stop when the soil is dry and alert me that it's time to water this particular plant so no noticed 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 w 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 well 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 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 all then let's say while the moisture is greater than 20 while the soil is still wet I will sample let say once per day the moisture and then report to myself the moisture that I need to 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 uh 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 all right so why don't I go ahead and maybe try this program now I'll run python of water. piy 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 20 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 why I try this again that seem to take only 3 days so really soil moisture content can vary based on the weather the sun Etc and it might really be somewhat random how dry or wet my soil uh becomes over time now here the magic of the wild Loop is that no matter when this happens whether it's 4 days in 2 days in 5 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 zero and then every time I iterate I'll add one to days so going from zero to one then one to two every day I sample my soil again and again and I could actually make it clear what day it is so 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 5 days indexing from zero this time it took 6 days indexing from zero and this time well it only took 3 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 right have a plant you're not sure when to water this was our short on wire Loops we'll see you next time
Original Description
***
This is CS50, Harvard University's introduction to the intellectual enterprises of computer science and the art of programming.
***
HOW TO SUBSCRIBE
http://www.youtube.com/subscription_center?add_user=cs50tv
HOW TO TAKE CS50
edX: https://cs50.edx.org/
Harvard Extension School: https://cs50.harvard.edu/extension
Harvard Summer School: https://cs50.harvard.edu/summer
OpenCourseWare: https://cs50.harvard.edu/x
HOW TO JOIN CS50 COMMUNITIES
Discord: https://discord.gg/cs50
Ed: https://cs50.edx.org/ed
Facebook Group: https://www.facebook.com/groups/cs50/
Faceboook Page: https://www.facebook.com/cs50/
GitHub: https://github.com/cs50
Gitter: https://gitter.im/cs50/x
Instagram: https://instagram.com/cs50
LinkedIn Group: https://www.linkedin.com/groups/7437240/
LinkedIn Page: https://www.linkedin.com/school/cs50/
Medium: https://cs50.medium.com/
Quora: https://www.quora.com/topic/CS50
Reddit: https://www.reddit.com/r/cs50/
Slack: https://cs50.edx.org/slack
Snapchat: https://www.snapchat.com/add/cs50
SoundCloud: https://soundcloud.com/cs50
Stack Exchange: https://cs50.stackexchange.com/
Telegram: https://t.me/cs50x
Threads: https://www.threads.net/@cs50
TikTok: https://www.tiktok.com/@cs50
Twitter: https://twitter.com/cs50
Twitter Community: https://twitter.com/i/communities/1722308663522594923
YouTube: http://www.youtube.com/cs50
HOW TO FOLLOW DAVID J. MALAN
Facebook: https://www.facebook.com/dmalan
GitHub: https://github.com/dmalan
Instagram: https://www.instagram.com/davidjmalan/
LinkedIn: https://www.linkedin.com/in/malan/
Quora: https://www.quora.com/profile/David-J-Malan
Threads: https://www.threads.net/@davidjmalan
TikTok: https://www.tiktok.com/@davidjmalan
Twitter: https://twitter.com/davidjmalan
***
CS50 SHOP
https://cs50.harvardshop.com/
***
LICENSE
CC BY-NC-SA 4.0
Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International Public License
https://creativecommons.org/licenses/by-nc-sa/4.0/
David J. Malan
https://cs.harvard.edu/malan
m
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from CS50 · CS50 · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Hello, World: Hadi Partovi
CS50
Content Distribution and Archival in a Digital Age
CS50
CS50 2014 - Week 1
CS50
CS50 2014 - Week 3
CS50
CS50 2014 - Week 0, continued
CS50
CS50 2014 - Week 4
CS50
Week 3, continued
CS50
Quiz 0 Review
CS50
CS50 2014 - Week 3, continued
CS50
CS50 2014 - Week 7
CS50
CS50 2014 - Week 7, continued
CS50
Breaking Through The (Google) Glass Ceiling by Christopher Bartholomew
CS50
Introduction to Amazon Web Services by Leo Zhadanovsky
CS50
CS50 2014 - Week 9
CS50
How to Build Innovative Technologies by Abby Fichtner
CS50
Light Your World (with Hue Bulbs) by Dan Bradley
CS50
Building Dynamic Web Apps with Laravel by Eric Ouyang
CS50
CS50 2014 - CS50 Lecture by Steve Ballmer
CS50
CS50 2014 - Week 10
CS50
This is CS50 with Steve Ballmer?
CS50
Meteor: a better way to build apps by Roger Zurawicki
CS50
Data Analysis in R by Dustin Tran
CS50
Data Visualization and D3 by David Chouinard
CS50
CS50 2014 - Week 6
CS50
Build Tomorrow's Library by Jeffrey Licht
CS50
CS50 2014 - Week 9, continued
CS50
Essential Scale-Out Computing by James Cuff
CS50
iOS App Development with Swift by Dan Armendariz
CS50
Sam Clark Leads Yale Students on Tour to CS50 at Harvard
CS50
3D Modeling and Manufacture by Ansel Duff
CS50
CS50 2014 - Week 5, continued
CS50
hello, world
CS50
CS50 2014 - Deep Thoughts - Hash Table
CS50
CS50 2014 - Deep Thoughts - Binary Tree
CS50
CS50 2014 - Deep Thoughts - Scratch
CS50
CS50 2014 - Deep Thoughts - MySQL
CS50
LaunchCode Visits CS50
CS50
CS50 Live, Episode 100
CS50
CS50 Field Trip to Google
CS50
This is CS50 AP
CS50
Week 4: Monday - CS50 2011 - Harvard University
CS50
Week 2: Wednesday - CS50 2011 - Harvard University
CS50
Week 1: Wednesday - CS50 2011 - Harvard University
CS50
Week 11: Monday - CS50 2011 - Harvard University
CS50
Week 3: Wednesday - CS50 2011 - Harvard University
CS50
Week 12: Monday - CS50 2011 - Harvard University
CS50
Week 1: Friday - CS50 2011 - Harvard University
CS50
Week 3: Monday - CS50 2011 - Harvard University
CS50
Week 10: Wednesday - CS50 2011 - Harvard University
CS50
Week 2: Monday - CS50 2011 - Harvard University
CS50
Week 9: Monday - CS50 2011 - Harvard University
CS50
Week 7: Monday - CS50 2011 - Harvard University
CS50
Week 5: Monday - CS50 2011 - Harvard University
CS50
Week 5: Wednesday - CS50 2011 - Harvard University
CS50
Week 7: Wednesday - CS50 2011 - Harvard University
CS50
Week 8: Monday - CS50 2011 - Harvard University
CS50
Week 9: Wednesday - CS50 2011 - Harvard University
CS50
Week 8: Wednesday - CS50 2011 - Harvard University
CS50
Week 10: Monday - CS50 2011 - Harvard University
CS50
Week 2: Wednesday - CS50 2010 - Harvard University
CS50
More on: Algorithm Basics
View skill →Related Reads
📰
📰
📰
📰
The Run-Length Encoding Trick: How Simple Strings Get Compressed
Medium · Programming
75 Days of Leetcode — Day 4: #238 — Product of Array Except Self
Medium · AI
I implemented the algorithm that broke the sorting barrier. Dijkstra still wins.
Medium · Programming
I implemented the algorithm that broke the sorting barrier. Dijkstra still wins.
Medium · Python
🎓
Tutor Explanation
DeepCamp AI