{"id":231,"date":"2020-11-10T10:12:00","date_gmt":"2020-11-10T10:12:00","guid":{"rendered":"http:\/\/lamiaiftekhar.com\/?page_id=231"},"modified":"2020-11-10T10:12:00","modified_gmt":"2020-11-10T10:12:00","slug":"20-minutes-python-day-14","status":"publish","type":"page","link":"http:\/\/lamiaiftekhar.com\/index.php\/20-minutes-python-day-14\/","title":{"rendered":"20-Minutes Python: Day 14"},"content":{"rendered":"<body>\n<p class=\"wp-block-paragraph\">What comes after \u2018for\u2019 loops? \u2018While\u2019 loops, yay!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">While loops keep repeating until some Boolean condition is met.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The general format is:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">while someCondition:<br>do stuff<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Once again, note the colon, then the tab in the next line<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Type and run<\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>desiredFloor=10<br>currentFloor=2<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>while currentFloor!=desiredFloor:<br>print(\u201cKeep going up!\u201d)<br>currentFloor = currentFloor+1<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>print(\u201cYou have reached the %dth Floor! The lift is opening.\u201d %desiredFloor)<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\" start=\"2\"><li>We could also do some basic number sequences like the previous day<\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">Type and run:<br><strong>num = 200<br>while num&gt;195:<br>print(num)<br>num = num-1<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\" start=\"3\"><li>Change the code above so that it also prints 195. Do not change any numbers, just change the Boolean operator.<\/li><li>What if you forget to write num = num-1 in the code of 2? Num will always be 200, and thus always be greater than 195. And it will be a dreaded infinite loop!<\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">No worries \u2013 you can stop an infinite loop on Jupyter Notebook by clicking Kernel \u2013&gt;Interrupt. Then restart the Kernel.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Kernel is a small piece of code that runs at the backend when you run your code. Sometimes when the kernel dies for reasons such as some large code or an internet disruption \u2013 your code starts being weird and doesn\u2019t run. In such cases, it is good to restart the kernel. Restarting the kernel is often the brute force way to fix things. Think of it as the equivalent of hitting an old-style TV until it starts showing clearer images (you are too young for this reference, perhaps \ud83d\ude1b )<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Take a deep breath and try the infinite loop! Then stop it by interrupting and restarting the kernel.<\/p>\n\n\n\n<ol class=\"wp-block-list\" start=\"5\"><li>Infinite loops are often unintended ( although there are some good ways to use them \u2013 we will learn later ). Consider the same code as #2. Suppose you have written everything correctly but made the tiny little mistake of typing a plus sign instead of a minus sign when updating the num value.<\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">Run it. It will give you an infinite loop of course. Scroll down and try to catch up to the end of the printed list \u2013 as soon as you reach the end, you will see numbers are being generated and printed continuously and you have to keep scrolling down to stay at the end.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Kill it.<\/p>\n\n\n\n<ol class=\"wp-block-list\" start=\"6\"><li>Ok, one more. This time we make an infinite loop using a simple Boolean.<\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">Type and run<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>while True:<br>print(\u2018Kurosaki-kun!\u2019)<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">There are smarter ways to kill this, by using an if condition and something called \u2018break\u2019 but we will learn that next time. For the time being just do the Kernel thing.<\/p>\n\n\n\n<ol class=\"wp-block-list\" start=\"7\"><li>Exercise<\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">Write a code that will give you the following output when run. Utilize a while loop.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>10!<br>9!<br>8!<br>7!<br>6!<br>5!<br>4!<br>3!<br>2!<br>1!<br>Blast off!<\/strong><\/p>\n<\/body>","protected":false},"excerpt":{"rendered":"<p>What comes after \u2018for\u2019 loops? \u2018While\u2019 loops, yay! While loops keep repeating until some Boolean condition is met. The general format is: while someCondition:do stuff Once again, note the colon, then the tab in the next line Type and run desiredFloor=10currentFloor=2 while currentFloor!=desiredFloor:print(\u201cKeep going up!\u201d)currentFloor = currentFloor+1 print(\u201cYou have reached the %dth Floor! The lift [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"class_list":["post-231","page","type-page","status-publish","hentry","entry"],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"http:\/\/lamiaiftekhar.com\/index.php\/wp-json\/wp\/v2\/pages\/231","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/lamiaiftekhar.com\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"http:\/\/lamiaiftekhar.com\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"http:\/\/lamiaiftekhar.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/lamiaiftekhar.com\/index.php\/wp-json\/wp\/v2\/comments?post=231"}],"version-history":[{"count":1,"href":"http:\/\/lamiaiftekhar.com\/index.php\/wp-json\/wp\/v2\/pages\/231\/revisions"}],"predecessor-version":[{"id":232,"href":"http:\/\/lamiaiftekhar.com\/index.php\/wp-json\/wp\/v2\/pages\/231\/revisions\/232"}],"wp:attachment":[{"href":"http:\/\/lamiaiftekhar.com\/index.php\/wp-json\/wp\/v2\/media?parent=231"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}