1
00:00:00,390 --> 00:00:03,969
All right, so the next challenge gets a little bit harder.

2
00:00:04,110 --> 00: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,

3
00:00:10,200 --> 00:00:13,950
pentagon, hexagon, heptagon, octagon, nonagon, and decagon.

4
00:00:14,220 --> 00: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

5
00:00:21,180 --> 00:00:26,340
color, and each of the sides are going to be 100 in terms of length,

6
00:00:26,700 --> 00:00:34,470
and the final outcome should look something like this. All of these shapes overlaid on each other and

7
00:00:34,470 --> 00:00:36,390
drawn out in sequence.

8
00:00:37,140 --> 00:00:42,900
So you'll have to think a little bit about this because we know that a square has 90-degree angles.

9
00:00:43,230 --> 00:00:48,980
So we could get our robot to go forwards and then turn right 90 degrees and then go forwards again.

10
00:00:49,470 --> 00:00:51,430
But what about a Pentagon?

11
00:00:51,690 --> 00:00:52,890
Well, a pentagon,

12
00:00:52,890 --> 00:00:56,210
each of the corners has a 72 degree angle.

13
00:00:57,390 --> 00:00:59,330
So what's the actual relationship?

14
00:00:59,340 --> 00:01:00,570
How can we work this out?

15
00:01:01,110 --> 00:01:05,700
Well, we know that to turn a full circle, it's 360 degrees.

16
00:01:06,150 --> 00: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.

17
00:01:14,730 --> 00:01:17,410
So 360 divided by five is 72.

18
00:01:17,490 --> 00:01:20,700
So each of the corners is 72 degrees.

19
00:01:21,570 --> 00:01:27,000
Using this knowledge and what you've learned about Turtle, go ahead and see if you can complete this

20
00:01:27,000 --> 00:01:27,570
challenge.

21
00:01:27,750 --> 00:01:29,400
Pause video now and give it a go.

22
00:01:32,880 --> 00: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

23
00:01:36,840 --> 00: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

24
00:01:43,560 --> 00:01:51,550
know that if we have 360 degrees divided by the number of sides, then we get the angle, right?

25
00:01:51,780 --> 00:01:55,620
So let's save that into a variable called angle.

26
00:01:56,520 --> 00:02:02,840
And then all we have to do is to get our robot to go forwards

27
00:02:04,350 --> 00: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

28
00:02:13,800 --> 00:02:16,320
the one that we defined at the top there.

29
00:02:17,010 --> 00:02:22,680
So then we just need this to be repeated for as many times as we have number of sides.

30
00:02:23,580 --> 00:02:25,830
So we could create a for loop, for example.

31
00:02:27,520 --> 00:02:31,120
And the range is going to take into account the number of sites.

32
00:02:32,650 --> 00: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,

33
00:02:40,090 --> 00:02:42,050
then this would be the code.

34
00:02:42,640 --> 00:02:44,170
So let's shift that along.

35
00:02:44,530 --> 00:02:47,200
And now if we go ahead and run this code,

36
00:02:48,680 --> 00: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

37
00:02:56,900 --> 00:02:58,220
different sides?

38
00:02:58,700 --> 00: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?

39
00:03:08,780 --> 00:03:13,220
To do this, instead of setting the number of sides statically like this,

40
00:03:13,530 --> 00:03:16,700
why don't we create a function? Let's call it draw_shape.

41
00:03:17,270 --> 00:03:24,470
And this function is going to take the number of sides that this shape has as an input.

42
00:03:24,980 --> 00:03:29,660
And then the rest of this code can be indented inside the function.

43
00:03:30,260 --> 00:03:33,890
And it probably makes sense to only calculate the angle once

44
00:03:34,130 --> 00:03:39,080
when we need to draw the shape because for the given number of sides, that angle is going to be the

45
00:03:39,080 --> 00:03:41,340
same across the entire shape.

46
00:03:42,500 --> 00:03:48,950
For example, if it was a square then we're going to go from one to four drawing this four times.

47
00:03:49,700 --> 00: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

48
00:03:56,960 --> 00:03:59,270
different number of sides that we need.

49
00:03:59,870 --> 00:04:06,560
Now, if we want a range of numbers between 3 and 10, we actually need to write the function 3,

50
00:04:06,560 --> 00:04:10,700
11, because the stop number is excluded

51
00:04:10,710 --> 00:04:14,150
so it'll stop at 10. As we loop through this range.

52
00:04:14,570 --> 00:04:21,010
this shape_side_n is going to take on the value depending on where we are in the range.

53
00:04:21,350 --> 00: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

54
00:04:26,630 --> 00:04:33,380
this shape_side_n into our draw_shape function and that should go from 3 to 10.

55
00:04:33,920 --> 00: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

56
00:04:40,970 --> 00:04:44,000
on going until it draws all of the shapes that we need.

57
00:04:45,020 --> 00:04:51,030
Now, the very last part of the challenge is to make each of the lines a different random color.

58
00:04:51,560 --> 00:04:53,090
So how can we do this?

59
00:04:54,440 --> 00: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

60
00:04:59,270 --> 00:05:01,140
colors in Python.

61
00:05:01,370 --> 00:05:08,510
I first tried to Google this without the python, and I got the different colors of various turtles,

62
00:05:08,840 --> 00:05:10,280
which was really fascinating

63
00:05:10,280 --> 00:05:11,980
but that's not what I wanted.

64
00:05:12,560 --> 00:05:14,270
So if we take a look at the first link

65
00:05:14,630 --> 00:05:21,260
this comes from a website called Trinket, and it has this interactive color palette where you can select

66
00:05:21,260 --> 00:05:27,740
one of these colors that you like and then you'll get the turtle name, which then you can copy and

67
00:05:27,740 --> 00:05:30,230
paste into maybe a list.

68
00:05:30,740 --> 00:05:31,910
Let's call it colors.

69
00:05:34,180 --> 00: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

70
00:05:39,940 --> 00:05:41,200
short as you want it to be.

71
00:05:41,860 --> 00:05:44,490
So here's a list that I created earlier.

72
00:05:44,950 --> 00: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

73
00:05:56,020 --> 00:06:03,160
a random color from this list which we know to be as simple as simply importing the random module

74
00:06:03,550 --> 00:06:06,880
and then we can get hold of the random. choice

75
00:06:07,240 --> 00:06:14,080
and then inside that method, we can pass in our list of colors so that it picks a random color from

76
00:06:14,080 --> 00:06:14,680
that list.

77
00:06:15,280 --> 00:06:16,990
So now let's run it again,

78
00:06:16,990 --> 00: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.

79
00:06:24,610 --> 00:06:25,680
So there you have it.

80
00:06:26,020 --> 00:06:28,680
That's the solution to this challenge.

81
00:06:29,140 --> 00:06:34,330
And this was a little bit more challenging I would say because we're doing functions and loops and

82
00:06:34,330 --> 00:06:37,780
you have to get hold of some colors and you have to figure all of that out.

83
00:06:38,050 --> 00:06:42,700
But hopefully you've managed just fine and you're ready to move on to the next challenge.

