{"id":234,"date":"2020-11-10T10:17:10","date_gmt":"2020-11-10T10:17:10","guid":{"rendered":"http:\/\/lamiaiftekhar.com\/?page_id=234"},"modified":"2020-11-10T10:17:10","modified_gmt":"2020-11-10T10:17:10","slug":"20-minutes-python-day-16","status":"publish","type":"page","link":"http:\/\/lamiaiftekhar.com\/index.php\/20-minutes-python-day-16\/","title":{"rendered":"20-Minutes Python: Day 16"},"content":{"rendered":"<body>\n<p class=\"wp-block-paragraph\">We continue loop today and add the concepts of \u2018break\u2019 and \u2018continue\u2019<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Break is used to exit a loop entirely.<br>Continue is used to exit only the current iteration.<\/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>count= 0<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>while True:<br>if count==5:<br>break<br>print(\u2018Kurosaki-kun\u2019)<br>count = count+1<br>print(\u201cTime for the next episode\u201d)<br>print(count)<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The last two print commands are outside the loop. The first print command and the counter update is inside the loop . Take note of the indentation. The if condition is of course inside the loop.<\/p>\n\n\n\n<ol class=\"wp-block-list\" start=\"2\"><li><br>Type and run<\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>bandListA = [\u2018Artcell\u2019,\u2019Aurthohin\u2019,\u2019AC\/DC\u2019,\u2019Arbovirus\u2019]<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>for band in bandListA:<br>if band==\u2019AC\/DC\u2019:<br>continue<br>print(\u2018%s is a Bangladeshi band\u2019 %band)<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\" start=\"3\"><li>Type and run<\/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>proposedNames = [\u2018Iron Man\u2019, \u2018Black Widow\u2019, \u2018Captain America\u2019, \u2018Quick Silver\u2019, \u2018Firestar\u2019, \u2018Thor\u2019, \u2018Invisible Woman\u2019, \u2018Scarlet Witch\u2019, \u2018Black Panther\u2019, \u2018Captain Marvel\u2019,\u2019Falcon\u2019,\u2019She-Hulk\u2019]<br>confirmedList =[]<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>for name in proposedNames:<br>if name==\u2019Quick Silver\u2019 or name==\u2019Scarlet Witch\u2019:<br>continue<br>elif name==\u2019Captain Marvel\u2019:<\/strong><br><strong>confirmedList.append(name)<br>print(\u2018We got Captain Marvel on the team! We need no more!!\u2019)<br>break<br>else:<br>confirmedList.append(name)<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>print(\u201cThe confirmed team for the mission is as follows:\u201d)<br>print(confirmedList)<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\" start=\"4\"><li>You could have done the above with two if conditions instead of if-elif-else. Also, you could have defined a separate list of names as \u2018RejectList\u2019 and use \u2018continue\u2019 to ignore them. Or you could just do this:<\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>confirmedList =[] # resetting confirmedList as an emptyLIst. ProposedNames is as before so not defining again<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>for name in proposedNames:<br>if name in [\u2018Quick Silver\u2019,\u2019Scarlet Witch\u2019]:<br>continue<br>if name==\u2019Captain Marvel\u2019:<br>confirmedList.append(name)<br>print(\u2018We got Captain Marvel on the team! We need no more!!\u2019)<br>break<br>confirmedList.append(name)<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>print(\u201cThe confirmed team for the mission is as follows:\u201d)<br>print(confirmedList)<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">What is the difference from the previous code in #3?<\/p>\n\n\n\n<ol class=\"wp-block-list\" start=\"5\"><li>When is an infinite loop setup intentional? Mostly in cases where we have a safety measure such as \u2018break\u2019 to come of the loop when a certain condition is met (well, in that case, we can\u2019t really call our setup an infinite loop, right? Let\u2019s call it a fake infinite loop \ud83d\ude1b )<\/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>from random import randint #we are importing a function \u2018randint\u2019<br>#from a special library \u2018random\u2019<br>#that will help us generate random numbers<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>while True:<br>someNumber = randint(1,6) #generates a number between 1 to 6 inclusive<br>print(someNumber)<br>if someNumber==6:<br>print(\u2018Chokka!!!!\u2019)<br>break<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Try running the cell where you have written the above code several times! You will get different outputs each time \ud83d\ude00<\/p>\n\n\n\n<ol class=\"wp-block-list\" start=\"6\"><li>We now make the code above a little more upgraded to depict real-time ludu playing feelings!<\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">Type and run. Well, run several times \ud83d\ude00<br>(You have already imported randint \u2013 so not importing it again).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>chaal=0<br>while True:<br>someNumber = randint(1,6) #generates a number between 1 to 20 inclusive<br>print(someNumber)<br>chaal=chaal+1<br>if someNumber==6:<br>print(\u2018Chokka!!!!\u2019)<br>break<br>if chaal%4==0:<br>print(\u2018Uff, why no chokka yet! :\/ \u2018)<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">What does the last if condition mean?<\/p>\n<\/body>","protected":false},"excerpt":{"rendered":"<p>We continue loop today and add the concepts of \u2018break\u2019 and \u2018continue\u2019 Break is used to exit a loop entirely.Continue is used to exit only the current iteration. Type and run count= 0 while True:if count==5:breakprint(\u2018Kurosaki-kun\u2019)count = count+1print(\u201cTime for the next episode\u201d)print(count) The last two print commands are outside the loop. The first print command [&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-234","page","type-page","status-publish","hentry","entry"],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"http:\/\/lamiaiftekhar.com\/index.php\/wp-json\/wp\/v2\/pages\/234","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=234"}],"version-history":[{"count":1,"href":"http:\/\/lamiaiftekhar.com\/index.php\/wp-json\/wp\/v2\/pages\/234\/revisions"}],"predecessor-version":[{"id":235,"href":"http:\/\/lamiaiftekhar.com\/index.php\/wp-json\/wp\/v2\/pages\/234\/revisions\/235"}],"wp:attachment":[{"href":"http:\/\/lamiaiftekhar.com\/index.php\/wp-json\/wp\/v2\/media?parent=234"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}