WEBVTT

0
00:00.390 --> 00:03.970
All right, so the next challenge gets a little bit harder.

1
00:04.110 --> 00:10.200
It's like we're doing our sit ups now. In this challenge, you're going to be drawing a triangle, square,

2
00:10.200 --> 00:13.950
pentagon, hexagon, heptagon, octagon, nonagon, and decagon.

3
00:14.220 --> 00:21.180
So from three-sided shape to ten-sided shape and each of those shapes is going to be drawn with a random

4
00:21.180 --> 00:26.340
color, and each of the sides are going to be 100 in terms of length,

5
00:26.700 --> 00:34.470
and the final outcome should look something like this. All of these shapes overlaid on each other and

6
00:34.470 --> 00:36.390
drawn out in sequence.

7
00:37.140 --> 00:42.900
So you'll have to think a little bit about this because we know that a square has 90-degree angles.

8
00:43.230 --> 00:48.980
So we could get our robot to go forwards and then turn right 90 degrees and then go forwards again.

9
00:49.470 --> 00:51.430
But what about a Pentagon?

10
00:51.690 --> 00:52.890
Well, a pentagon,

11
00:52.890 --> 00:56.210
each of the corners has a 72 degree angle.

12
00:57.390 --> 00:59.330
So what's the actual relationship?

13
00:59.340 --> 01:00.570
How can we work this out?

14
01:01.110 --> 01:05.700
Well, we know that to turn a full circle, it's 360 degrees.

15
01:06.150 --> 01:14.370
So the way you work out the angle of each of these shapes is by dividing 360 by the number of sides.

16
01:14.730 --> 01:17.410
So 360 divided by five is 72.

17
01:17.490 --> 01:20.700
So each of the corners is 72 degrees.

18
01:21.570 --> 01:27.000
Using this knowledge and what you've learned about Turtle, go ahead and see if you can complete this

19
01:27.000 --> 01:27.570
challenge.

20
01:27.750 --> 01:29.400
Pause video now and give it a go.

21
01:32.880 --> 01:36.840
All right, so I'm going to delete the previous code and we're going to write the code that's going

22
01:36.840 --> 01:43.560
to draw all of these shapes. The first thing I want to do is I want to put down that equation. So we

23
01:43.560 --> 01:51.550
know that if we have 360 degrees divided by the number of sides, then we get the angle, right?

24
01:51.780 --> 01:55.620
So let's save that into a variable called angle.

25
01:56.520 --> 02:02.840
And then all we have to do is to get our robot to go forwards

26
02:04.350 --> 02:13.800
by 100, we said. And then to get it to turn right and the angle that it's going to turn right by is

27
02:13.800 --> 02:16.320
the one that we defined at the top there.

28
02:17.010 --> 02:22.680
So then we just need this to be repeated for as many times as we have number of sides.

29
02:23.580 --> 02:25.830
So we could create a for loop, for example.

30
02:27.520 --> 02:31.120
And the range is going to take into account the number of sites.

31
02:32.650 --> 02:39.520
So now if the number of sites is equal to let's say five, let's say we're trying to draw a pentagon,

32
02:40.090 --> 02:42.050
then this would be the code.

33
02:42.640 --> 02:44.170
So let's shift that along.

34
02:44.530 --> 02:47.200
And now if we go ahead and run this code,

35
02:48.680 --> 02:56.900
you can see it draws us a pentagon, a five-sided shape. So how can we get this loop to run for the

36
02:56.900 --> 02:58.220
different sides?

37
02:58.700 --> 03:07.550
How can we get it to go from a three-sided shape, a triangle, all the way to a ten-sided shape, a decagon?

38
03:08.780 --> 03:13.220
To do this, instead of setting the number of sides statically like this,

39
03:13.530 --> 03:16.700
why don't we create a function? Let's call it draw_shape.

40
03:17.270 --> 03:24.470
And this function is going to take the number of sides that this shape has as an input.

41
03:24.980 --> 03:29.660
And then the rest of this code can be indented inside the function.

42
03:30.260 --> 03:33.890
And it probably makes sense to only calculate the angle once

43
03:34.130 --> 03:39.080
when we need to draw the shape because for the given number of sides, that angle is going to be the

44
03:39.080 --> 03:41.340
same across the entire shape.

45
03:42.500 --> 03:48.950
For example, if it was a square then we're going to go from one to four drawing this four times.

46
03:49.700 --> 03:56.960
So now later on, we can define another for loop and this for loop is going to go through all of the

47
03:56.960 --> 03:59.270
different number of sides that we need.

48
03:59.870 --> 04:06.560
Now, if we want a range of numbers between 3 and 10, we actually need to write the function 3,

49
04:06.560 --> 04:10.700
11, because the stop number is excluded

50
04:10.710 --> 04:14.150
so it'll stop at 10. As we loop through this range.

51
04:14.570 --> 04:21.010
this shape_side_n is going to take on the value depending on where we are in the range.

52
04:21.350 --> 04:26.630
So when we're first drawing this, we're going to draw a triangle with three sides so then we can pass

53
04:26.630 --> 04:33.380
this shape_side_n into our draw_shape function and that should go from 3 to 10.

54
04:33.920 --> 04:40.970
So let's run our code and see it draw our triangle first and then our square and then it just keeps

55
04:40.970 --> 04:44.000
on going until it draws all of the shapes that we need.

56
04:45.020 --> 04:51.030
Now, the very last part of the challenge is to make each of the lines a different random color.

57
04:51.560 --> 04:53.090
So how can we do this?

58
04:54.440 --> 04:59.270
So, of course, we're going to go to our good friend Google and we're going to search for the turtle

59
04:59.270 --> 05:01.140
colors in Python.

60
05:01.370 --> 05:08.510
I first tried to Google this without the python, and I got the different colors of various turtles,

61
05:08.840 --> 05:10.280
which was really fascinating

62
05:10.280 --> 05:11.980
but that's not what I wanted.

63
05:12.560 --> 05:14.270
So if we take a look at the first link

64
05:14.630 --> 05:21.260
this comes from a website called Trinket, and it has this interactive color palette where you can select

65
05:21.260 --> 05:27.740
one of these colors that you like and then you'll get the turtle name, which then you can copy and

66
05:27.740 --> 05:30.230
paste into maybe a list.

67
05:30.740 --> 05:31.910
Let's call it colors.

68
05:34.180 --> 05:39.940
And you can put as many colors into this palette as you want, and you can make it as extensive or as

69
05:39.940 --> 05:41.200
short as you want it to be.

70
05:41.860 --> 05:44.490
So here's a list that I created earlier.

71
05:44.950 --> 05:55.990
And now, if we go ahead and set our turtle's color, so let's go ahead and get Tim and set his color to

72
05:56.020 --> 06:03.160
a random color from this list which we know to be as simple as simply importing the random module

73
06:03.550 --> 06:06.880
and then we can get hold of the random. choice

74
06:07.240 --> 06:14.080
and then inside that method, we can pass in our list of colors so that it picks a random color from

75
06:14.080 --> 06:14.680
that list.

76
06:15.280 --> 06:16.990
So now let's run it again,

77
06:16.990 --> 06:23.860
and you can see each time the shape is being drawn it's picking a random color from our list of colors.

78
06:24.610 --> 06:25.680
So there you have it.

79
06:26.020 --> 06:28.680
That's the solution to this challenge.

80
06:29.140 --> 06:34.330
And this was a little bit more challenging I would say because we're doing functions and loops and

81
06:34.330 --> 06:37.780
you have to get hold of some colors and you have to figure all of that out.

82
06:38.050 --> 06:42.700
But hopefully you've managed just fine and you're ready to move on to the next challenge.