1 00:00:00,180 --> 00:00:05,180 Now the final challenge for today comes courtesy of a childhood toy that you 2 00:00:05,190 --> 00:00:08,310 might've already seen. So this is going to be our training bike, 3 00:00:08,610 --> 00:00:12,330 and it's going to be the last coding exercise before we tackle our final 4 00:00:12,330 --> 00:00:16,500 project. So what are we doing? We're making a spirograph. 5 00:00:16,800 --> 00:00:19,350 Do you remember those geometric gear shaped things 6 00:00:19,350 --> 00:00:23,160 which you would draw and draw and it would make these funky circular shapes? 7 00:00:23,490 --> 00:00:25,980 Well, that's what we're going to be doing, but with turtle. 8 00:00:26,400 --> 00:00:31,400 We're going to get our turtle to draw a number of circles each with a radius of 9 00:00:32,040 --> 00:00:35,250 a hundred in distance. And once it's done, 10 00:00:35,280 --> 00:00:39,060 we should end up with something that looks like this. 11 00:00:39,900 --> 00:00:43,710 So we're going to be using our random colors and we're going to be using the 12 00:00:43,710 --> 00:00:44,640 documentation. 13 00:00:45,540 --> 00:00:50,070 I want you to dig through the documentation and see if you can figure out how to 14 00:00:50,070 --> 00:00:54,330 draw a circle and to determine how large the circle should be. 15 00:00:55,020 --> 00:00:59,910 And then I want you to figure out how you can get it to repeatedly draw a circle 16 00:01:00,180 --> 00:01:05,129 and change the tilt of the circle by a little bit each time, 17 00:01:05,370 --> 00:01:09,930 so that you end up with that effect. Pause the video and give that a go now. 18 00:01:12,960 --> 00:01:13,770 Alright. Again, 19 00:01:13,770 --> 00:01:16,530 we're going to be scrolling through all of the available methods 20 00:01:16,590 --> 00:01:18,960 looking for something that might help us. 21 00:01:19,800 --> 00:01:23,790 I think it's going to be somewhere in the turtle motion's move and draw section. 22 00:01:24,150 --> 00:01:25,740 And if you look down this list, 23 00:01:25,770 --> 00:01:30,770 you can see there is a method that allows us to draw a circle with a given 24 00:01:31,110 --> 00:01:31,943 radius. 25 00:01:32,070 --> 00:01:37,070 So we can put in the radius as one of the inputs and draw our circle. Heading 26 00:01:37,950 --> 00:01:39,420 back to our code, 27 00:01:39,450 --> 00:01:44,450 I'm going to delete some of the previous parts where we've got the directions 28 00:01:44,970 --> 00:01:46,710 and also changing the pen size. 29 00:01:47,040 --> 00:01:51,090 I'm going to leave the speed as fastest because we're going to need it to go 30 00:01:51,090 --> 00:01:53,430 pretty quick to draw all of those circles. 31 00:01:53,910 --> 00:01:56,400 And I'm also going to delete the final for loop. 32 00:01:57,270 --> 00:02:01,710 Now notice how I've got this warning here telling me that it doesn't really like 33 00:02:01,740 --> 00:02:06,740 the idea of me calling a variable the same as the name of a function. 34 00:02:07,470 --> 00:02:10,889 So why don't we just call this color instead 35 00:02:11,250 --> 00:02:16,230 and then we can return that as the output and get rid of that warning. 36 00:02:16,860 --> 00:02:20,490 Now we're ready to code up our spirograph. 37 00:02:20,820 --> 00:02:25,170 So the first thing that we're going to try and do is to draw a circle. 38 00:02:25,650 --> 00:02:29,100 Now the circle requires a radius, 39 00:02:29,130 --> 00:02:33,570 so I'm going to give it a radius of a hundred. And when I hit run, 40 00:02:33,810 --> 00:02:37,080 we can see briefly that it painted a circle. 41 00:02:37,530 --> 00:02:42,060 So let's go ahead and create our screen from t.screen. 42 00:02:42,660 --> 00:02:47,660 And then we can use that screen.exitonclick method so that when we run our 43 00:02:48,480 --> 00:02:53,130 code, it doesn't actually remove the window until we click on it. 44 00:02:53,880 --> 00:02:57,120 So I can see that I've painted my first circle 45 00:02:57,300 --> 00:03:01,750 and I would like, if possible, to make my circle 46 00:03:01,960 --> 00:03:04,060 have a different color each time. 47 00:03:04,420 --> 00:03:09,420 So I'm going to change the color to use a random color that's produced by this 48 00:03:10,030 --> 00:03:13,630 function. So now every time I run my code, 49 00:03:13,690 --> 00:03:16,270 you'll see a different colored circle. 50 00:03:17,050 --> 00:03:22,050 The turtle is basically gonna draw out this path and then go back to its 51 00:03:22,870 --> 00:03:25,600 original position facing East. 52 00:03:26,020 --> 00:03:29,740 If we wanted multiple circles to overlap with each other, 53 00:03:29,860 --> 00:03:34,150 then we're going to have to change the heading or the pointing direction of our 54 00:03:34,150 --> 00:03:37,450 turtle. If we look at this section, 55 00:03:37,750 --> 00:03:40,300 the current state of our turtle, 56 00:03:40,540 --> 00:03:43,420 you can see that you can get the current position, 57 00:03:43,450 --> 00:03:46,900 the current Y coordinate and X coordinate, 58 00:03:47,110 --> 00:03:49,450 but you can also get hold of the current heading, 59 00:03:49,480 --> 00:03:52,150 so which direction it's pointing towards. 60 00:03:52,720 --> 00:03:55,840 And all we have to do is just call this heading method. 61 00:03:56,770 --> 00:04:01,210 So now if I print my tim.heading, 62 00:04:01,720 --> 00:04:06,720 then you can see that when I run my code and I take a look inside the console, 63 00:04:07,030 --> 00:04:09,400 it's pointing at 0.0. 64 00:04:10,090 --> 00:04:15,040 What if we could change the heading to make it a little bit shifted? 65 00:04:15,670 --> 00:04:15,880 Well, 66 00:04:15,880 --> 00:04:20,740 there's actually a different method for changing the heading and getting the 67 00:04:20,740 --> 00:04:25,570 heading. For example, if we want to set the heading, 68 00:04:25,600 --> 00:04:29,590 then we have to use this method and give it a number. 69 00:04:30,370 --> 00:04:32,890 So what if I get the current heading 70 00:04:35,980 --> 00:04:39,580 and then maybe I add 5 or 10 degrees to it, 71 00:04:39,910 --> 00:04:44,050 and then I change my turtle's heading using set 72 00:04:44,050 --> 00:04:47,560 heading to the current heading plus 10. 73 00:04:49,450 --> 00:04:54,190 And then we get a new circle drawn. So let's see what this looks like. 74 00:04:55,840 --> 00:04:59,830 And you can see that I've drawn one circle and then I've tilted to the left a 75 00:04:59,830 --> 00:05:03,940 little bit and then drawn a second circle. So extrapolating this, 76 00:05:03,970 --> 00:05:06,610 you can imagine that if we had a loop that kept going, 77 00:05:06,850 --> 00:05:09,610 then we could draw our final spirograph shape. 78 00:05:10,180 --> 00:05:11,860 Let's create that loop. 79 00:05:12,160 --> 00:05:15,910 So the parts that don't need to be in the loop is setting the speed. 80 00:05:16,300 --> 00:05:18,880 But the color needs to change each time, 81 00:05:19,120 --> 00:05:23,410 the circle needs to be drawn each time and the heading needs to be updated each 82 00:05:23,410 --> 00:05:24,243 time. 83 00:05:24,280 --> 00:05:29,280 Now I'm going to simplify this line of code to contract it into a single line. 84 00:05:31,540 --> 00:05:34,870 So I'm setting Tim to a new heading 85 00:05:35,230 --> 00:05:38,290 and that is from the current heading plus 10. 86 00:05:39,250 --> 00:05:43,000 So now let's create our loop and let's say 87 00:05:43,000 --> 00:05:45,550 we're going to loop a hundred times. 88 00:05:48,540 --> 00:05:51,900 Now let's run our code and see what it looks like. 89 00:05:54,090 --> 00:05:56,910 You can see it's drawing out our spirograph. 90 00:05:56,930 --> 00:06:00,350 It's not as dense as it previously was, 91 00:06:00,830 --> 00:06:02,330 but look at what it's doing. 92 00:06:02,390 --> 00:06:06,410 It's drawing the same circle on top of a previous circle, 93 00:06:06,650 --> 00:06:08,510 cause it doesn't know when to stop. 94 00:06:09,200 --> 00:06:13,790 It's stopping only once it's gone through the hundred rotations. 95 00:06:14,270 --> 00:06:19,270 So how can we figure out a way to get it to only draw as many circles as we 96 00:06:19,730 --> 00:06:20,563 need? 97 00:06:21,740 --> 00:06:26,740 Let's say that I created a new function called draw_spirograph, 98 00:06:27,890 --> 00:06:31,520 and this function accepts a single parameter, 99 00:06:31,790 --> 00:06:35,390 which is the size of the gap. 100 00:06:37,550 --> 00:06:42,260 And that is going to be the size that's going to be in between each of these 101 00:06:42,260 --> 00:06:43,490 circles that gets drawn. 102 00:06:44,000 --> 00:06:48,230 And that's of course determined by the direction heading of my arrow. 103 00:06:48,890 --> 00:06:51,710 So if I pass in this size of gap, 104 00:06:51,740 --> 00:06:56,450 I'm going to use it here to change the tilt of my circle. 105 00:06:57,020 --> 00:07:02,020 But I can also use that because it's a degree to determine how many times my 106 00:07:02,780 --> 00:07:04,460 spirograph needs to be drawn. 107 00:07:04,910 --> 00:07:09,440 So we know that there's 360 degrees in a full circle. 108 00:07:09,980 --> 00:07:12,980 And for each circle that we're drawing, 109 00:07:13,310 --> 00:07:17,720 we're basically giving it a different offset right? And 110 00:07:17,720 --> 00:07:19,070 that's our size_of_gap. 111 00:07:19,490 --> 00:07:22,760 So if we divide 360 by each offset, 112 00:07:23,120 --> 00:07:28,120 then we'll repeat our code for as many times as we need to draw all the circles 113 00:07:28,760 --> 00:07:31,940 and complete the final spirograph. Now, 114 00:07:32,000 --> 00:07:35,960 the only problem is if I call this method, 115 00:07:36,770 --> 00:07:40,460 dra, I think I meant to write draw. 116 00:07:41,540 --> 00:07:45,410 So draw spirograph and I give it a size of five. 117 00:07:45,470 --> 00:07:47,750 So a little bit smaller than last time, 118 00:07:48,050 --> 00:07:52,100 tilting a little bit less each time. You can see that if I run this code, 119 00:07:52,490 --> 00:07:53,780 we get an error. 120 00:07:54,350 --> 00:07:59,350 And the error says that the float object can't be used as an integer 121 00:08:00,650 --> 00:08:03,380 and it's telling us where we have that float object. 122 00:08:03,950 --> 00:08:08,240 So remember that this number is going to go into here 123 00:08:08,300 --> 00:08:13,220 and then 360 divided by five is actually going to give us a floating point 124 00:08:13,220 --> 00:08:16,070 number, even though it divides cleanly 125 00:08:16,100 --> 00:08:19,340 just because this is how division works in Python. Now, 126 00:08:19,370 --> 00:08:24,370 unfortunately though our range function is going to need a whole number, 127 00:08:25,160 --> 00:08:26,030 an integer. 128 00:08:26,360 --> 00:08:30,890 It can't take a floating point number because then how many times are we 129 00:08:30,890 --> 00:08:34,070 repeating it? 3.4 times? What does that even mean? 130 00:08:34,370 --> 00:08:37,130 So this input has to be a whole number. 131 00:08:37,520 --> 00:08:42,520 So let's turn it into an integer by creating an integer and then wrapping this 132 00:08:44,059 --> 00:08:48,470 calculation inside the brackets. So now when we run our code, 133 00:08:48,500 --> 00:08:53,500 you can see it paints our spirograph, shifting by five degrees each time. 134 00:08:54,710 --> 00:08:59,670 And once it's gone through all of the iterations and the spirograph is 135 00:08:59,670 --> 00:09:02,280 complete, then it actually stops. 136 00:09:03,750 --> 00:09:07,050 Did you manage to get the solution for this challenge? 137 00:09:07,500 --> 00:09:12,500 If not, be sure to review this lesson and fix your code as needed. Once you're 138 00:09:13,020 --> 00:09:15,600 happy with everything that we've explained so far, 139 00:09:16,050 --> 00:09:19,080 then it's time to head to the final challenge. 140 00:09:19,530 --> 00:09:22,560 Let's go ahead and create a million dollar painting. 141 00:09:23,160 --> 00:09:26,400 So for all of that and more, I'll see you on the next lesson.