1 00:00:13,020 --> 00:00:15,920 How's everybody doing, OK? 2 00:00:16,350 --> 00:00:24,490 I think it's time to begin giving you the answers of the homework once you tried to get the answers 3 00:00:24,490 --> 00:00:25,300 about yourselves. 4 00:00:25,440 --> 00:00:28,610 If you get into trouble, I'll just you want to verify your results. 5 00:00:28,620 --> 00:00:30,390 You can use this lecture to do it. 6 00:00:30,930 --> 00:00:35,250 OK, so first of all, create a new file, you can call it while loops practice. 7 00:00:35,520 --> 00:00:36,930 And from here, let's begin. 8 00:00:37,380 --> 00:00:40,420 So, first of all, let's add and comment here. 9 00:00:41,940 --> 00:00:44,730 Let's call it while loops practice. 10 00:00:46,440 --> 00:00:52,260 And as you recall, you have bought a Barbie and PERTZYE. 11 00:00:55,210 --> 00:01:04,570 All right, like this and first thing first, you were asked to create a list in poverty and use it 12 00:01:04,570 --> 00:01:10,480 along the way, the items were five, six, twenty four, thirty to twenty one and seventy. 13 00:01:11,020 --> 00:01:11,340 Right. 14 00:01:11,680 --> 00:01:15,300 And also, we need to put some spaces here. 15 00:01:15,790 --> 00:01:18,420 So Python would not be angry at us. 16 00:01:18,430 --> 00:01:18,970 All right. 17 00:01:20,170 --> 00:01:20,770 Like this. 18 00:01:21,190 --> 00:01:22,240 It's nice and beautiful. 19 00:01:23,380 --> 00:01:29,040 And from here, let us create our first wire loop of a OK. 20 00:01:29,050 --> 00:01:31,960 And right here we want to ages to be tested. 21 00:01:32,350 --> 00:01:41,710 And our stop condition is as long as ages is smaller than 30, we should continue on. 22 00:01:41,740 --> 00:01:42,160 All right. 23 00:01:42,370 --> 00:01:46,360 But in this way, we we don't actually test these cells. 24 00:01:46,780 --> 00:01:51,400 So how can we actually extract the cells out of the out of the list? 25 00:01:52,060 --> 00:01:52,470 All right. 26 00:01:52,690 --> 00:01:57,040 So we need to combine some knowledge out of the lists, lectures that we have seen. 27 00:01:57,310 --> 00:02:02,590 And the only way to extract values out of the list is using an index. 28 00:02:02,830 --> 00:02:03,150 Right. 29 00:02:03,460 --> 00:02:05,260 Like doing something like this. 30 00:02:05,630 --> 00:02:08,290 OK, so this is the way to extract values. 31 00:02:08,470 --> 00:02:16,390 So we need somehow to put out a way to enter some parameter here so we could extract values by index. 32 00:02:17,110 --> 00:02:23,260 And if we put, for example, a simple number here, so always this value would be tested again and 33 00:02:23,260 --> 00:02:23,560 again. 34 00:02:23,770 --> 00:02:26,230 But we need something else. 35 00:02:26,230 --> 00:02:30,370 We need something else that on each cycle, this number here would grow. 36 00:02:31,420 --> 00:02:37,540 OK, and the only way to do it is basically to create an additional variable called counter, it's exactly 37 00:02:37,540 --> 00:02:42,930 the hint that it gives you in the previous video and here to place the counter. 38 00:02:43,240 --> 00:02:46,810 So for starts, this value would be zero. 39 00:02:47,020 --> 00:02:47,380 All right. 40 00:02:48,190 --> 00:02:55,780 And from here, let's Sprint Party, for example, for for beginning part A. to have some indication. 41 00:02:55,960 --> 00:03:03,250 And the age is ages counter, OK, with the simple casting here. 42 00:03:03,760 --> 00:03:04,110 All right. 43 00:03:04,450 --> 00:03:07,810 So anybody could guess what will happen if I leave it like this. 44 00:03:08,340 --> 00:03:10,030 OK, let's run it and see. 45 00:03:11,790 --> 00:03:17,580 All right, so we're running this and you can see something happening here is just keeps running and 46 00:03:17,580 --> 00:03:18,060 running. 47 00:03:18,460 --> 00:03:24,240 It will keep running even after the end will after the world will end. 48 00:03:24,360 --> 00:03:25,590 It will keep running. 49 00:03:25,920 --> 00:03:33,450 Or maybe my computer could explode by the result of this because it's not going to stop for for never. 50 00:03:33,490 --> 00:03:33,860 Right. 51 00:03:34,740 --> 00:03:38,040 The reason is, let me stop it before something happens. 52 00:03:38,490 --> 00:03:38,890 All right. 53 00:03:38,910 --> 00:03:40,110 This is what you see here. 54 00:03:41,070 --> 00:03:44,400 This is the classic occasion of endless loop. 55 00:03:44,670 --> 00:03:50,700 You can see the value of five is printed endlessly, probably printed several thousand times for now. 56 00:03:51,600 --> 00:03:52,550 And what happens here? 57 00:03:52,560 --> 00:03:59,080 Basically, we have a discount or tested and the value inside the edges is always zero. 58 00:03:59,310 --> 00:04:07,020 So this value is extracted for eternity because we didn't manage to grow this number by one on each 59 00:04:07,020 --> 00:04:07,500 cycle. 60 00:04:07,710 --> 00:04:13,590 So stuck on zero here and basically print the same value again and again and again endlessly. 61 00:04:13,710 --> 00:04:22,450 OK, so this is a classic, a classic case of endless loop and you must avoid it because this is fail, 62 00:04:22,690 --> 00:04:23,970 failed programming. 63 00:04:24,210 --> 00:04:26,520 You cannot leave loops like this. 64 00:04:26,830 --> 00:04:35,070 OK, so what you want to do here is to make it like this, OK, you want to grow the counter by one 65 00:04:35,070 --> 00:04:38,340 on each cycle and if we run it now. 66 00:04:40,070 --> 00:04:43,010 Simply, you seeing that it's not endless right now. 67 00:04:43,760 --> 00:04:53,810 All right, so as you can see, the stop the stop conditioning here works and it stops as far as to 68 00:04:53,810 --> 00:04:56,600 this value right here, because this value is above 30. 69 00:04:56,840 --> 00:05:03,080 And this is the reason that these values are not printed, because once the values inside here and sell 70 00:05:03,080 --> 00:05:09,990 the Aegis counter is bigger, is bigger than 30, and once this thing became false. 71 00:05:10,580 --> 00:05:13,220 So the wire stops working. 72 00:05:13,550 --> 00:05:20,310 And also you were asked to print outside of the wire loop and the value. 73 00:05:20,360 --> 00:05:20,810 All right. 74 00:05:21,170 --> 00:05:28,220 Print me part A, the value that caused. 75 00:05:31,710 --> 00:05:34,140 Caused the loop to stop. 76 00:05:34,690 --> 00:05:35,250 OK. 77 00:05:36,790 --> 00:05:47,080 So this one was ages counter, and the reason is this printout is outside, the wire loop is right here. 78 00:05:47,080 --> 00:05:48,600 You can see an invention, right? 79 00:05:48,850 --> 00:05:50,560 We declared the wire loop right here. 80 00:05:51,010 --> 00:05:53,290 And every line, it has indention here. 81 00:05:53,290 --> 00:05:55,660 The spaces is inside the way loop. 82 00:05:55,900 --> 00:06:00,110 But once we remove the indention and we go straight from the star here. 83 00:06:00,340 --> 00:06:06,490 So this line is outside the wire loop and we just want to check what number caused the wire to fail 84 00:06:06,490 --> 00:06:07,180 to go out. 85 00:06:07,690 --> 00:06:08,140 All right. 86 00:06:10,460 --> 00:06:17,210 OK, great, so you can see exactly what value caused it to fail, because the loop went again, again, 87 00:06:17,250 --> 00:06:22,790 again and again up until it got to this number and then it exited the wire loop because this statement 88 00:06:22,790 --> 00:06:31,450 was false because this value of 32 went up in here and 32 is not smaller than 30. 89 00:06:31,730 --> 00:06:37,700 And this is what caused the wall to exit and go straight to the flow of code outside the wire loop, 90 00:06:37,700 --> 00:06:38,630 which is Rikyu. 91 00:06:40,930 --> 00:06:49,960 All right, from here in Barbie, you were asked, first of all, to go an empty line before the outboards, 92 00:06:50,080 --> 00:06:55,720 and the simple reason is just to avoid here makes of outputs, just to have some spaces. 93 00:06:56,020 --> 00:07:00,910 And if you recall from previous lectures, it's done by men like this. 94 00:07:02,260 --> 00:07:03,160 Let's just check it. 95 00:07:04,750 --> 00:07:10,930 And you can see that if I put A, B, C here and I have the empty, empty line here, once we print 96 00:07:10,930 --> 00:07:11,170 it. 97 00:07:11,290 --> 00:07:14,770 Oops, once we print it. 98 00:07:15,220 --> 00:07:18,440 OK, so you see empty, empty line right here. 99 00:07:18,760 --> 00:07:20,470 OK, if I remove it. 100 00:07:24,660 --> 00:07:31,080 You see, it's go straight ahead after this printout right here, we don't have any spaces between these 101 00:07:31,080 --> 00:07:31,510 lines. 102 00:07:32,190 --> 00:07:34,250 OK, so we want a separation here. 103 00:07:36,170 --> 00:07:42,800 All right, so in PABI, you were asked to use the same or the same way, just going to copy it from 104 00:07:42,800 --> 00:07:43,130 here. 105 00:07:43,160 --> 00:07:44,630 No need to print it again. 106 00:07:45,500 --> 00:07:53,480 But this time you were also asked to put in if and stop the loop from inside. 107 00:07:53,690 --> 00:07:59,840 Once the ages lowers, the value inside the cells is bigger than 20. 108 00:08:01,100 --> 00:08:01,670 All right. 109 00:08:02,000 --> 00:08:10,100 So age, if if the value inside the list is bigger than 20, then what we want to do first of all, 110 00:08:10,250 --> 00:08:11,660 we want to print the value. 111 00:08:13,700 --> 00:08:20,870 First of all, Part B, bring the value of, uh, that stopped. 112 00:08:22,890 --> 00:08:28,350 The loop, OK, and it goes like this age is counter. 113 00:08:30,670 --> 00:08:33,920 And how do we stop the way from inside, OK? 114 00:08:34,060 --> 00:08:39,610 You had an underlying under the warden's side, so the way to do it is exactly what we have seen in 115 00:08:39,610 --> 00:08:46,290 the advanced lecture theory is use a break, OK, just to make sure the brake worked also. 116 00:08:46,540 --> 00:08:47,140 You know what? 117 00:08:48,880 --> 00:08:49,930 Let me ask you something. 118 00:08:50,080 --> 00:08:52,480 OK, before taking the break and everything. 119 00:08:52,630 --> 00:08:53,740 Let me ask you something. 120 00:08:55,150 --> 00:08:57,070 Is this is this code correct? 121 00:08:57,100 --> 00:08:58,920 Is this flow from top to bottom here? 122 00:08:58,930 --> 00:08:59,400 Correct. 123 00:09:01,250 --> 00:09:02,500 Something is wrong here. 124 00:09:02,590 --> 00:09:03,860 OK, I will give you a hint. 125 00:09:04,370 --> 00:09:07,060 We declare counter equals zero out here. 126 00:09:07,460 --> 00:09:12,840 Then we go over a few loops here and we go up until this line right here. 127 00:09:13,390 --> 00:09:13,800 Correct. 128 00:09:15,580 --> 00:09:21,140 So once we are finished with this loop, we go straight ahead to this to this place right here. 129 00:09:21,880 --> 00:09:24,850 What do you think the first value of counter is here? 130 00:09:24,850 --> 00:09:30,370 Whether do you think the first value that will appear inside these square brackets here, let's test. 131 00:09:30,910 --> 00:09:31,710 How do we do it? 132 00:09:32,200 --> 00:09:34,210 Let's just print counter. 133 00:09:34,470 --> 00:09:37,090 OK, I'm just going to I'm just one. 134 00:09:37,090 --> 00:09:41,220 I just want to check what value of counter will go here. 135 00:09:41,410 --> 00:09:44,620 So I'm going to print it before it got to the wild. 136 00:09:44,840 --> 00:09:47,420 I have a printout right here just for me. 137 00:09:47,470 --> 00:09:50,720 OK, it's debugging just for me to know what is the value here. 138 00:09:51,190 --> 00:09:53,160 OK, let's run it and see what happens. 139 00:09:54,770 --> 00:09:57,350 As you can see, the value here is three. 140 00:09:57,500 --> 00:10:00,690 OK, so this is wrong coding, this is wrong. 141 00:10:00,710 --> 00:10:09,800 This is false because we want to start the loop fresh from the first cell off the list and we starting 142 00:10:09,830 --> 00:10:12,680 here from here, OK? 143 00:10:12,890 --> 00:10:18,200 Because what we haven't done and we should do is just going to erase it. 144 00:10:18,500 --> 00:10:21,410 We should before starting another while loop. 145 00:10:21,590 --> 00:10:29,030 We should use calendar equals zero to zeros because once it's zero, it start from the cell, from the 146 00:10:29,690 --> 00:10:31,350 index, zero from here. 147 00:10:31,400 --> 00:10:37,910 OK, and if you do not put counter equals zero here, just going to start from the counter where it's 148 00:10:37,910 --> 00:10:42,290 left and we left it at value equals three. 149 00:10:42,560 --> 00:10:43,760 OK, and this is false. 150 00:10:45,380 --> 00:10:45,830 All right. 151 00:10:45,940 --> 00:10:52,880 And also, you can see the PABI here was not printed at all because as it got here to the beginning 152 00:10:52,880 --> 00:10:58,520 of the loop, it already went out of the loop because the first value that was accepted right here is 153 00:10:58,520 --> 00:10:59,240 32. 154 00:10:59,270 --> 00:11:06,500 And obviously this this gold right here didn't run event because it straight ahead went outside of the 155 00:11:06,500 --> 00:11:06,920 loop. 156 00:11:06,970 --> 00:11:13,540 OK, now that we have counter equals zero, let's just put it under Bombi. 157 00:11:15,250 --> 00:11:17,590 All right, so now we can run it. 158 00:11:19,270 --> 00:11:24,010 OK, still, we have some problem, you can see that nothing is printed right here. 159 00:11:24,340 --> 00:11:26,380 Let's see, why do we have this problem? 160 00:11:27,370 --> 00:11:28,380 Oh, all right. 161 00:11:29,080 --> 00:11:32,500 Also, we forgot something very important. 162 00:11:32,800 --> 00:11:35,710 Again, you forgot to do this. 163 00:11:36,970 --> 00:11:37,280 Right. 164 00:11:40,090 --> 00:11:40,690 All right. 165 00:11:41,850 --> 00:11:45,700 We are growing the counter variable by one on each cycle. 166 00:11:49,760 --> 00:11:51,230 And there you go. 167 00:11:51,620 --> 00:12:00,010 All right, so what we actually do here is use the break action key, as we have seen in the advanced 168 00:12:00,120 --> 00:12:05,860 report regarding why loops and what happens here is we stop the loop from inside. 169 00:12:05,870 --> 00:12:07,910 This was our mission, OK? 170 00:12:08,130 --> 00:12:18,800 And once the value was over 20, this was the cause of our why loop to stop running and as we were asked 171 00:12:18,800 --> 00:12:23,690 as well, were needed to print the value that caused this stop. 172 00:12:23,810 --> 00:12:28,010 OK, so the value caused the Y look to run is twenty four. 173 00:12:28,400 --> 00:12:31,750 Twenty four is indeed bigger than twenty. 174 00:12:32,000 --> 00:12:35,450 And this is what made the Y look to stop running. 175 00:12:35,630 --> 00:12:43,010 And as you can see, nothing else here was printed, only the value twenty four which is this printout. 176 00:12:43,070 --> 00:12:43,450 OK. 177 00:12:43,580 --> 00:12:48,290 And right after this print out we had a break here so nothing else. 178 00:12:49,010 --> 00:12:51,890 Rund ran inside this wall. 179 00:12:53,120 --> 00:12:59,660 OK, so the wire loop exited once you got to this break. 180 00:12:59,660 --> 00:13:00,080 Action. 181 00:13:01,280 --> 00:13:05,810 OK, and also the reason here that we have to intention's. 182 00:13:05,810 --> 00:13:06,170 All right. 183 00:13:06,890 --> 00:13:14,840 We have one indention using tab and second indention is because the code that we write up in here is 184 00:13:14,840 --> 00:13:17,360 applied under the if OK. 185 00:13:17,600 --> 00:13:22,610 And this thing, for example, the counter plus equals one. 186 00:13:22,940 --> 00:13:31,820 It has one indention because this one is applied under the while and the if does not have any any action 187 00:13:31,820 --> 00:13:32,960 taken upon it. 188 00:13:33,200 --> 00:13:37,300 OK, this thing runs under the wire, only under the wire. 189 00:13:38,330 --> 00:13:45,900 It's invisible to the IV here because only two indentations is the code that is visible to the IF. 190 00:13:47,680 --> 00:13:53,860 All right, so we have completed the second part, we have stopped the while loop from inside as we 191 00:13:53,860 --> 00:13:58,410 were asked and from here we can go straight ahead to assignment number three. 192 00:13:58,630 --> 00:13:59,920 That's do as well here. 193 00:13:59,950 --> 00:14:08,500 The print slash in just we have some order like order in my eyes, order here. 194 00:14:08,500 --> 00:14:18,130 I mean, inside the output and inside here, we again need to make the counter equal zero at the beginning. 195 00:14:19,180 --> 00:14:19,720 All right. 196 00:14:20,210 --> 00:14:28,390 And the thing is that we were asked to do here is, first of all, to create a wire loop and the wire 197 00:14:28,390 --> 00:14:35,080 loop should be with a stop stop condition of sanity like this. 198 00:14:36,620 --> 00:14:46,140 And the next thing we were asked to do is to add plus to OK, to the sales values. 199 00:14:46,160 --> 00:14:46,540 All right. 200 00:14:46,820 --> 00:14:51,610 So we're going to use ages counter, which is this is the sale value. 201 00:14:52,010 --> 00:14:58,190 And again, we're going to use ages counter and do it lasta. 202 00:14:58,400 --> 00:15:07,010 OK, so we're going to take the cell value add plus to it and all of this is going to go straight ahead 203 00:15:07,190 --> 00:15:09,050 inside this cell. 204 00:15:09,260 --> 00:15:16,520 OK, so this cell will change and it will have incited these values. 205 00:15:16,790 --> 00:15:26,720 OK, so once we have completed this thing, let's print out part B or C cells. 206 00:15:26,990 --> 00:15:34,050 New values value is ages counter. 207 00:15:34,550 --> 00:15:35,030 All right. 208 00:15:35,780 --> 00:15:45,650 And again, what we need to do not forget counter loss equals block plus equals one. 209 00:15:47,010 --> 00:15:55,630 All right, and also we were asked to use an else here and see what happens once all the while loop 210 00:15:55,650 --> 00:16:03,960 exits with a number, we should go to parts, see inside or else. 211 00:16:05,460 --> 00:16:08,400 Because of the number 212 00:16:11,010 --> 00:16:12,030 ages count. 213 00:16:13,320 --> 00:16:21,510 All right, so we have an exit point, meaning once this statement is false, so the while loop will 214 00:16:21,510 --> 00:16:25,420 exit itself and will go straight to the yells, OK. 215 00:16:25,470 --> 00:16:34,080 So in in our assignment a here, once the while loop exits, just goes outside of the wire loop completely 216 00:16:34,080 --> 00:16:35,250 goes outside of the wire. 217 00:16:35,460 --> 00:16:39,680 Like this line is not connected to the wire loop at all in any connection. 218 00:16:40,200 --> 00:16:47,010 But here in assignment number C we have created an else statement and once the wire loop will exit, 219 00:16:47,190 --> 00:16:51,290 it will go straight to the L's condition right here. 220 00:16:51,600 --> 00:16:52,590 Let's see what happens. 221 00:16:54,730 --> 00:16:57,770 All right, we have some kind of air in line here. 222 00:16:58,180 --> 00:17:05,440 All right, so let's try to understand it is good practice can only can get an eighth string at the 223 00:17:05,440 --> 00:17:06,430 end of the string. 224 00:17:06,460 --> 00:17:08,080 OK, we also can see here. 225 00:17:10,110 --> 00:17:12,730 Oh, we missed the guesting, of course. 226 00:17:14,920 --> 00:17:17,480 All right, they should be they should be OK. 227 00:17:20,130 --> 00:17:29,280 All right, so you can see here inside the house, so we have been successful resolving policy as well, 228 00:17:30,000 --> 00:17:33,490 what basically happened here, just enlarge it for a second. 229 00:17:33,900 --> 00:17:40,350 So basically what happened here, we went over each of these cells and we have added plus two to each 230 00:17:40,350 --> 00:17:40,770 of them. 231 00:17:40,920 --> 00:17:49,860 So five plus two is seven and six plus two is eight and so on and so on up until we got to number 70. 232 00:17:51,360 --> 00:18:00,150 And we have here a condition that once this number will be false, OK, once this statement will be 233 00:18:00,150 --> 00:18:03,940 false and in our case, 70 here. 234 00:18:04,200 --> 00:18:06,450 So this statement is false, obviously. 235 00:18:06,810 --> 00:18:13,050 And once we have 70 value right here, it will exceed the wire loop and go straight to the yells. 236 00:18:13,230 --> 00:18:20,310 And you can see the reason of getting into really is this number right here, 70, which caused the 237 00:18:20,310 --> 00:18:21,310 wire to exit. 238 00:18:21,810 --> 00:18:22,260 All right. 239 00:18:23,340 --> 00:18:25,570 I think we're done at this point. 240 00:18:25,710 --> 00:18:28,680 This was a bit challenging assignments, I must admit. 241 00:18:29,820 --> 00:18:33,690 But I think it's good to have some challenges in your life, as always. 242 00:18:33,910 --> 00:18:38,990 And also, I just want to mention and remind you again that the reason that you're doing this course 243 00:18:39,030 --> 00:18:40,000 is not for fun. 244 00:18:40,320 --> 00:18:46,100 The reason that you're doing it is to learn Python to become professionals and to become python ninjas. 245 00:18:46,890 --> 00:18:49,240 Some of you want to become front and develop. 246 00:18:49,590 --> 00:18:53,190 Some of you want to become beckoned or even automation developers. 247 00:18:53,370 --> 00:18:55,860 And you must control and dominate these fields. 248 00:18:56,640 --> 00:19:02,880 And I assure you that this is the the highest level of programming regarding these topics that you should 249 00:19:02,880 --> 00:19:10,500 know tricks that you're seeing here in the assignments and in the theory and in the example part, this 250 00:19:10,500 --> 00:19:17,700 will all will be applied once you will start working as programmers and once you will start developing 251 00:19:17,700 --> 00:19:18,720 your own software. 252 00:19:19,800 --> 00:19:26,370 These tricks and these that knowledge will help you a lot, because now I can assure you that you dominate 253 00:19:26,550 --> 00:19:31,840 also the loop's section regarding Python programming language. 254 00:19:32,970 --> 00:19:33,450 All right. 255 00:19:33,450 --> 00:19:39,930 So once we have completed this part, now it's time to go forward to the next topic. 256 00:19:40,140 --> 00:19:42,180 And I will see you in the next video. 257 00:19:42,390 --> 00:19:43,570 Thank you and bye bye. 258 00:19:43,860 --> 00:19:44,520 Good job.