1 00:00:00,510 --> 00:00:03,270 Instructor: All right, your head must be spinning 2 00:00:03,270 --> 00:00:05,880 because of all these looping. 3 00:00:05,880 --> 00:00:07,410 That's a really bad joke. 4 00:00:07,410 --> 00:00:09,510 But I thought it was funny because I've been recording 5 00:00:09,510 --> 00:00:11,940 for hours now and I think I'm going a little kooky. 6 00:00:11,940 --> 00:00:15,060 So this is gonna be my last video for the day, 7 00:00:15,060 --> 00:00:16,320 not for the course, 8 00:00:16,320 --> 00:00:19,110 but we're gonna do a little fun exercise. 9 00:00:19,110 --> 00:00:21,300 Now, I call all exercises fun. 10 00:00:21,300 --> 00:00:25,620 Obviously I'm biased, but this one's especially fun. 11 00:00:25,620 --> 00:00:27,570 What are we going to do? 12 00:00:27,570 --> 00:00:31,290 Well, we're going to simulate what a computer does 13 00:00:31,290 --> 00:00:35,550 when we have something like a graphical user interface. 14 00:00:35,550 --> 00:00:38,329 That is, a computer is able to display, 15 00:00:38,329 --> 00:00:40,440 let's say even an image over here, right? 16 00:00:40,440 --> 00:00:42,780 I can see the robot over here. 17 00:00:42,780 --> 00:00:44,430 I can see the mouse. 18 00:00:44,430 --> 00:00:46,380 This is a graphical user interface. 19 00:00:46,380 --> 00:00:49,110 I see pictures, I see images on my screen, 20 00:00:49,110 --> 00:00:53,070 these pixels on my screen, and I can control them. 21 00:00:53,070 --> 00:00:56,280 Well, we're gonna create a basic version of this 22 00:00:56,280 --> 00:00:59,970 to show you how a computer would work 23 00:00:59,970 --> 00:01:02,550 just from the stuff that we've learned up until now 24 00:01:02,550 --> 00:01:05,550 using loops and using conditional logic. 25 00:01:05,550 --> 00:01:07,083 So what are we going to do? 26 00:01:08,160 --> 00:01:11,010 Well, this is the exercise, 27 00:01:11,010 --> 00:01:13,920 and you can check out the ripple here 28 00:01:13,920 --> 00:01:16,800 or you can recreate this, or again, in this video, 29 00:01:16,800 --> 00:01:18,900 you should have resources attached 30 00:01:18,900 --> 00:01:22,290 that you can play with it yourself. 31 00:01:22,290 --> 00:01:24,270 And what I want you to do is I want you 32 00:01:24,270 --> 00:01:27,960 to loop through this list of lists 33 00:01:27,960 --> 00:01:32,310 and every time you encounter a zero, I want you to display 34 00:01:32,310 --> 00:01:35,710 on the screen over here an empty space 35 00:01:37,140 --> 00:01:41,220 because a zero in a computer denotes nothing, right? 36 00:01:41,220 --> 00:01:43,740 We want a blank on the screen. 37 00:01:43,740 --> 00:01:48,270 However, when I see a one, I wanna simulate a pixel, right, 38 00:01:48,270 --> 00:01:51,120 that tiny, tiny dot on our computers 39 00:01:51,120 --> 00:01:52,500 that can be full of colors. 40 00:01:52,500 --> 00:01:55,140 It could be green, it can be blue, it can be orange. 41 00:01:55,140 --> 00:01:58,350 But for our case, I want it to be a star. 42 00:01:58,350 --> 00:02:00,000 And using that, 43 00:02:00,000 --> 00:02:03,570 I want you to create a program that takes this picture 44 00:02:03,570 --> 00:02:05,400 that let's say is in our database 45 00:02:05,400 --> 00:02:07,980 or in our computer's hard drive and displays it 46 00:02:07,980 --> 00:02:12,960 on the screen using space or multiply. 47 00:02:12,960 --> 00:02:16,290 And all I want you to do is when I click run, 48 00:02:16,290 --> 00:02:20,280 I want you to display that image right here. 49 00:02:20,280 --> 00:02:21,810 All right, this is a little tough one 50 00:02:21,810 --> 00:02:24,210 and I'm kind of letting you figure out on your own. 51 00:02:24,210 --> 00:02:27,180 But trust me, you've learned all the tools necessary 52 00:02:27,180 --> 00:02:28,560 to do this. 53 00:02:28,560 --> 00:02:30,870 Pause the video, give it a go, 54 00:02:30,870 --> 00:02:33,070 otherwise, I'm gonna show you how it's done. 55 00:02:34,200 --> 00:02:36,180 So the very first thing I like to do 56 00:02:36,180 --> 00:02:39,000 is to think about what I'm about to do. 57 00:02:39,000 --> 00:02:40,950 I wanna make sure that we have a plan in mind. 58 00:02:40,950 --> 00:02:42,360 So I'm going to comment first, 59 00:02:42,360 --> 00:02:43,380 and I'm going to say, 60 00:02:43,380 --> 00:02:47,717 while we definitely wanna iterate over picture, right? 61 00:02:49,200 --> 00:02:51,630 So we're gonna do some sort of iteration here. 62 00:02:51,630 --> 00:02:53,643 So that's number one. 63 00:02:55,470 --> 00:03:00,470 And then in here, I wanna say that if it's a zero, 64 00:03:03,420 --> 00:03:06,603 then I wanna print an empty space. 65 00:03:07,800 --> 00:03:12,800 And if it's a one, I wanna print a star, 66 00:03:17,490 --> 00:03:18,323 right? 67 00:03:18,323 --> 00:03:19,620 So that's the plan. 68 00:03:19,620 --> 00:03:23,280 Okay, so this shouldn't be too hard. 69 00:03:23,280 --> 00:03:27,960 Actually, before you get started, in order for this to work, 70 00:03:27,960 --> 00:03:31,290 you need to google a special parameter 71 00:03:31,290 --> 00:03:35,850 or a special option that you can give the print function. 72 00:03:35,850 --> 00:03:37,050 And as a matter of fact, 73 00:03:37,050 --> 00:03:40,110 the print function that you need to add 74 00:03:40,110 --> 00:03:44,970 or the option that you need to add is this end option. 75 00:03:44,970 --> 00:03:48,210 And if we scroll down, it doesn't display it that well. 76 00:03:48,210 --> 00:03:52,890 But you can see here, string appended after the last value. 77 00:03:52,890 --> 00:03:56,700 So the default when we print something is a new line 78 00:03:56,700 --> 00:04:00,600 but ideally, we might not want a new line. 79 00:04:00,600 --> 00:04:05,600 So when that happens, you might have to use this end option. 80 00:04:06,090 --> 00:04:07,980 Now, I know I left it really vague. 81 00:04:07,980 --> 00:04:10,650 I want you to practice Googling this. 82 00:04:10,650 --> 00:04:12,570 And figuring out from your mistake, 83 00:04:12,570 --> 00:04:15,210 you're gonna have a bug when you create this code, 84 00:04:15,210 --> 00:04:18,420 when you try to display this information. 85 00:04:18,420 --> 00:04:22,110 But try to solve it using the tools that you have 86 00:04:22,110 --> 00:04:23,313 of problem solving. 87 00:04:24,210 --> 00:04:25,043 All right. 88 00:04:25,043 --> 00:04:26,103 Enough talk. Let's get to it. 89 00:04:27,150 --> 00:04:30,213 So the first thing I wanna do is iterate over the picture. 90 00:04:31,290 --> 00:04:36,290 I'm going to say for image in picture because, 91 00:04:36,990 --> 00:04:40,980 well, this is one picture right here. 92 00:04:40,980 --> 00:04:43,830 So this is the picture, but it's inside of a list 93 00:04:43,830 --> 00:04:46,590 because we can have multiple pictures. 94 00:04:46,590 --> 00:04:49,770 And then, I'm going to loop once again 95 00:04:49,770 --> 00:04:52,470 over this individual list. 96 00:04:52,470 --> 00:04:55,200 So once again, I need to do a for loop. 97 00:04:55,200 --> 00:04:57,540 And this time I'm going to say for pixel 98 00:04:57,540 --> 00:05:02,540 because each one of this is a pixel in the image. 99 00:05:03,510 --> 00:05:06,060 So this is a nested for loop. 100 00:05:06,060 --> 00:05:08,820 Now, I'm going to add a conditional and say, 101 00:05:08,820 --> 00:05:13,820 if pixel equals one, 102 00:05:14,490 --> 00:05:19,490 then I'm going to print a star. 103 00:05:24,480 --> 00:05:29,480 Otherwise, I'm going to print an empty string. 104 00:05:33,270 --> 00:05:35,940 Well, maybe not empty, right? 105 00:05:35,940 --> 00:05:40,940 Because we do want a space, a blank space in the image. 106 00:05:41,970 --> 00:05:44,793 All right, so let's see if this works. 107 00:05:46,170 --> 00:05:51,170 If I click run, hmm, that's not really what I wanted. 108 00:05:52,800 --> 00:05:54,810 I want a clear image here, 109 00:05:54,810 --> 00:05:58,050 but I'm just getting things one in line. 110 00:05:58,050 --> 00:06:01,500 And remember, this was the little trick 111 00:06:01,500 --> 00:06:06,033 where every time it prints, it creates a new line. 112 00:06:07,620 --> 00:06:12,483 The default, again, if we close this, you'll see, 113 00:06:13,470 --> 00:06:18,470 is that end equals to this escape sequence of new line. 114 00:06:19,110 --> 00:06:24,110 So we can change that by simply saying, star, comma, end. 115 00:06:25,980 --> 00:06:28,110 And then I'm going to say for the end, 116 00:06:28,110 --> 00:06:29,100 I don't want a new line. 117 00:06:29,100 --> 00:06:33,483 I just want, well, a string, but an empty string. 118 00:06:34,500 --> 00:06:36,003 And same over here. 119 00:06:37,110 --> 00:06:38,193 If I do comma, 120 00:06:41,760 --> 00:06:43,623 end, and if I click run. 121 00:06:45,720 --> 00:06:48,480 All right, it's getting a little bit better 122 00:06:48,480 --> 00:06:52,590 but now I have an issue where we don't have any new lines. 123 00:06:52,590 --> 00:06:55,260 Everything is on one line. 124 00:06:55,260 --> 00:06:56,910 Hmm, that's not good. 125 00:06:56,910 --> 00:06:58,920 How can we solve this? 126 00:06:58,920 --> 00:07:03,870 Well, ideally, at the end of this first loop 127 00:07:03,870 --> 00:07:08,760 where we're just going the image or the line in the image, 128 00:07:08,760 --> 00:07:13,760 at the end of this, we create a new line right here, right? 129 00:07:14,520 --> 00:07:16,890 So again, looking at the indentation, 130 00:07:16,890 --> 00:07:19,630 I don't want a new line on every pixel 131 00:07:20,940 --> 00:07:22,500 which we had the first time. 132 00:07:22,500 --> 00:07:27,500 But I also wanna add a line between the lists of rows. 133 00:07:29,070 --> 00:07:32,940 So maybe a better name for this will be a row in picture. 134 00:07:32,940 --> 00:07:37,940 And in here, I'm going to add at the end of this for loop, 135 00:07:38,880 --> 00:07:42,933 so after we're done looping through the entire row, 136 00:07:44,010 --> 00:07:48,240 the bottom here, I'm going to print an empty line. 137 00:07:48,240 --> 00:07:51,213 But remember, the default is gonna be a new line. 138 00:07:52,380 --> 00:07:53,670 So again, an empty string, 139 00:07:53,670 --> 00:07:56,040 it's going to default to a new line. 140 00:07:56,040 --> 00:07:58,020 And we're still gonna have this code. 141 00:07:58,020 --> 00:08:03,020 If I run this, I get an error because image is not defined 142 00:08:05,010 --> 00:08:07,140 because I've just changed this to a row. 143 00:08:07,140 --> 00:08:10,200 So let's do row here. 144 00:08:10,200 --> 00:08:11,073 Click run, 145 00:08:13,320 --> 00:08:14,280 and look at that. 146 00:08:14,280 --> 00:08:16,800 We have our beautiful Christmas tree. 147 00:08:16,800 --> 00:08:18,300 If you don't think this is beautiful, 148 00:08:18,300 --> 00:08:19,800 this is the best I could. 149 00:08:19,800 --> 00:08:21,963 But this is a Christmas tree. 150 00:08:23,130 --> 00:08:25,050 And we finally have it displaying. 151 00:08:25,050 --> 00:08:27,840 We've used loops, we've used conditional logic, 152 00:08:27,840 --> 00:08:30,120 and we used a little Googling to figure out, 153 00:08:30,120 --> 00:08:33,059 hey, we need this option. 154 00:08:33,059 --> 00:08:38,010 Now our code works, but I wanna do something better here. 155 00:08:38,010 --> 00:08:41,400 And in the next video, I wanna clean up this code 156 00:08:41,400 --> 00:08:45,120 a little bit and cover a very important topic when it comes 157 00:08:45,120 --> 00:08:46,680 to programming. 158 00:08:46,680 --> 00:08:47,760 I'll see you in that one. 159 00:08:47,760 --> 00:08:48,593 Bye bye.