1
00:00:00,330 --> 00:00:05,250
So in the last lesson, we saw how to create a turtle that does a random walk.

2
00:00:05,760 --> 00:00:07,020
Now in this lesson,

3
00:00:07,080 --> 00:00:12,080
I want to show you how you can create a random color for your turtle drawing

4
00:00:12,540 --> 00:00:16,890
rather than just using the named colors and then picking a random one out of the

5
00:00:16,890 --> 00:00:21,390
list. When you take a look at the pen color of the turtle,

6
00:00:21,810 --> 00:00:26,810
you can see that you can use a color string such as what we've been doing using

7
00:00:26,940 --> 00:00:30,420
red, yellow, or a hex string that represents the color,

8
00:00:31,020 --> 00:00:33,690
or we can use a RGB color.

9
00:00:34,170 --> 00:00:39,170
And the RGB color is going to be represented by a tuple of different amounts of

10
00:00:39,510 --> 00:00:43,560
red, green, and blue. So what exactly is a tuple?

11
00:00:44,310 --> 00:00:44,760
Well,

12
00:00:44,760 --> 00:00:49,760
a tuple is a data type in Python and it looks like this.

13
00:00:50,130 --> 00:00:52,830
It has round brackets around it

14
00:00:53,190 --> 00:00:56,970
and then each of the items inside are separated by a comma.

15
00:00:57,570 --> 00:00:59,970
So does this remind you of another data type?

16
00:01:00,600 --> 00:01:04,110
Does it not look a little bit like a list? Well,

17
00:01:04,110 --> 00:01:08,880
indeed a tuple is very similar to a list. Each of the items that go into the

18
00:01:08,880 --> 00:01:10,260
tuple are ordered.

19
00:01:10,740 --> 00:01:15,720
So let's pull up my Python console here and let's create my tuple 

20
00:01:17,130 --> 00:01:21,060
which is denoted by a set of round parentheses

21
00:01:21,450 --> 00:01:25,410
and then let's put in three items, 1, 3, and 8,

22
00:01:26,010 --> 00:01:28,320
and there we have created a new tuple.

23
00:01:28,950 --> 00:01:32,370
Now in order to get hold of any of those items in there,

24
00:01:32,460 --> 00:01:36,540
I can write the name of the variable and then using square brackets

25
00:01:36,570 --> 00:01:38,460
I can access the index.

26
00:01:38,730 --> 00:01:43,710
So 1 is going to be at position zero, 3 is going to be at position one and

27
00:01:43,740 --> 00:01:47,220
8 is going to be at position two. So if I hit enter on this,

28
00:01:47,280 --> 00:01:49,350
then you can see that's going to be equal to 8.

29
00:01:50,820 --> 00:01:52,860
And if you click this drop down on the right,

30
00:01:52,890 --> 00:01:56,700
you can see the same representation; position 0, 1,  2,

31
00:01:57,060 --> 00:02:00,510
and those pieces of data and the total length is three.

32
00:02:01,110 --> 00:02:05,760
So at this point you might be asking yourself, well, I already know about lists,

33
00:02:05,820 --> 00:02:10,320
so why do I need to know about tuples? What's the difference anyways? Well,

34
00:02:10,350 --> 00:02:14,220
a tuple is going to be carved in stone

35
00:02:14,460 --> 00:02:18,390
so you can't change the values like you can with lists.

36
00:02:19,500 --> 00:02:22,020
For example, if I wanted to say

37
00:02:22,020 --> 00:02:26,220
get hold of my tuple and instead of the 8 at position two,

38
00:02:26,610 --> 00:02:30,630
I want to change it to 12. Now, if I go ahead and hit enter,

39
00:02:30,660 --> 00:02:34,350
you can see I get a error and this is a type error.

40
00:02:34,890 --> 00:02:39,390
This particular type, tuple, does not support item assignment.

41
00:02:39,600 --> 00:02:42,720
So I can't just change it willingly like this.

42
00:02:43,290 --> 00:02:46,380
And I also can't remove items from the tuple,

43
00:02:46,620 --> 00:02:51,330
I can't change it in any way. Once you've created your tuple 

44
00:02:51,360 --> 00:02:56,360
you can consider it as carved in stone and it is what we call immutable.

45
00:02:57,150 --> 00:03:01,900
It cannot be changed. So why would you use a tuple then?

46
00:03:02,620 --> 00:03:03,880
Well, think about the times

47
00:03:04,150 --> 00:03:07,990
like say if you're creating a color scheme for your website or are you creating

48
00:03:07,990 --> 00:03:12,220
some sort of list that you want to stay constant and you don't want somebody to

49
00:03:12,220 --> 00:03:16,360
accidentally change it or accidentally mess it up, well,

50
00:03:16,360 --> 00:03:18,610
then you might want to consider using a tuple.

51
00:03:20,050 --> 00:03:23,800
And if you find yourself creating a tuple and then realizing, Oh,

52
00:03:23,830 --> 00:03:26,020
actually I need to change it well,

53
00:03:26,020 --> 00:03:29,650
then you can simply put your tuple inside a list like this,

54
00:03:30,010 --> 00:03:32,800
and you can actually convert it into a list.

55
00:03:34,150 --> 00:03:36,010
Coming back to our documentation,

56
00:03:36,400 --> 00:03:41,400
the color is represented by a tuple because if you define a color,

57
00:03:41,680 --> 00:03:43,330
you're unlikely to change it.

58
00:03:44,020 --> 00:03:46,870
And then we have a r, g, and b,

59
00:03:46,960 --> 00:03:50,380
which is in the range between zero and the color mode.

60
00:03:50,860 --> 00:03:55,860
So the mode can be between 0 and 1 or 0 and 255. In the course resources,

61
00:03:58,030 --> 00:04:01,810
I've linked to this really handy RGB calculator tool,

62
00:04:02,290 --> 00:04:06,190
which demonstrates how RGB colors work so perfectly.

63
00:04:06,610 --> 00:04:10,060
You can see that if we have zero amount of red, zero amounts of green,

64
00:04:10,090 --> 00:04:11,920
zero amounts of blue, we get black.

65
00:04:12,430 --> 00:04:17,430
But if we mix different amounts of red and different amounts of green and

66
00:04:18,670 --> 00:04:22,029
different amounts of blue, then we can get all of the colors in the rainbow.

67
00:04:22,360 --> 00:04:26,020
The three primary colors can create any color that you dream of.

68
00:04:26,620 --> 00:04:29,110
And each of these sliders,

69
00:04:29,140 --> 00:04:34,000
you can see, goes from 0 all the way up to 255.

70
00:04:34,570 --> 00:04:38,230
So more red, more green or more blue.

71
00:04:39,220 --> 00:04:42,730
And this is what the RGB color ends up looking like.

72
00:04:43,930 --> 00:04:48,790
I find it easier to think about the range between 0 and 255.

73
00:04:49,330 --> 00:04:50,200
So in turtle,

74
00:04:50,200 --> 00:04:54,880
we actually have to change the color mode to 255.

75
00:04:55,510 --> 00:04:57,340
So to do this, it's a little bit tricky

76
00:04:57,370 --> 00:04:59,920
which is why I didn't want to put this as part of the challenge.

77
00:05:00,310 --> 00:05:04,750
But we have to tap into the actual turtle module and not the turtle object,

78
00:05:04,990 --> 00:05:09,910
and then change the color mode for that module. Back in our code,

79
00:05:09,940 --> 00:05:12,490
we can tap into our turtle module

80
00:05:12,820 --> 00:05:15,910
which is known as 't' in our code.

81
00:05:17,080 --> 00:05:22,080
And then we can tap into the color mode to make it go from 0 to 255.

82
00:05:24,610 --> 00:05:29,590
So now that we've changed the color mode, we can now create a random color.

83
00:05:30,040 --> 00:05:33,280
So let's get rid of this list of colors. Instead,

84
00:05:33,280 --> 00:05:36,880
I'm going to create a new function called a random_color,

85
00:05:37,960 --> 00:05:42,960
and this function is going to return a random color using RGB.

86
00:05:44,440 --> 00:05:47,410
So let's first create a random red.

87
00:05:48,280 --> 00:05:53,280
So we use the random module and then use the randint and then generate a number

88
00:05:53,620 --> 00:05:56,740
between 0 and 255.

89
00:05:57,710 --> 00:06:02,420
And then we can repeat this process for the other two color spaces,

90
00:06:02,750 --> 00:06:05,900
so green and for blue.

91
00:06:06,920 --> 00:06:08,930
Now we've got three random numbers,

92
00:06:08,960 --> 00:06:11,480
one each representing each of the color spaces,

93
00:06:11,840 --> 00:06:15,590
and then we can generate our tuple. Here's the challenge for you.

94
00:06:15,950 --> 00:06:18,290
See if you can return from this function

95
00:06:18,350 --> 00:06:23,350
a tuple that consists of the three random integers, r, g and b,

96
00:06:24,710 --> 00:06:29,710
and then use that random color to color the turtle drawing instead of this broken

97
00:06:30,920 --> 00:06:34,070
code which relied on that previous list of colors.

98
00:06:34,610 --> 00:06:37,220
Pause the video and see if you can complete this challenge.

99
00:06:40,150 --> 00:06:42,460
All right. So we know that to create a tuple 

100
00:06:42,460 --> 00:06:45,460
we need a set of round brackets or parentheses,

101
00:06:45,940 --> 00:06:48,610
and then we're going to put each of these in order,

102
00:06:48,610 --> 00:06:53,380
so r, g and b. So here's our tuple created,

103
00:06:53,410 --> 00:06:56,860
and this is basically our new random color.

104
00:06:57,460 --> 00:07:02,460
And then this function is going to return that random color as the output.

105
00:07:03,850 --> 00:07:08,350
So now instead of using random.choice and using that previous list,

106
00:07:08,860 --> 00:07:11,770
all we have to do is get hold of a random color

107
00:07:12,160 --> 00:07:16,840
that's going to be outputted from this function and then use it to color Tim's

108
00:07:16,840 --> 00:07:19,780
drawing. So let's run this code again,

109
00:07:20,320 --> 00:07:23,380
and you should be able to see that in this random walk,

110
00:07:23,500 --> 00:07:25,780
the color is completely random.

111
00:07:26,110 --> 00:07:31,110
Pretty much every single turn is going to draw something completely different in

112
00:07:31,540 --> 00:07:32,373
terms of color.

113
00:07:33,190 --> 00:07:38,080
And this is achieved by learning a little bit about how colors work and how we

114
00:07:38,080 --> 00:07:41,860
can generate them using the random RGB colors.

