WEBVTT

0
00:00.170 --> 00:06.560
So the code challenge that you're about to do comes from a really interesting phenomenon that you might

1
00:06.560 --> 00:08.120
find in London.

2
00:08.150 --> 00:13.850
I live in London and it's well known as a big financial district.

3
00:13.850 --> 00:20.810
So when you head over to the city or the financial area in London and you go into a restaurant,

4
00:20.810 --> 00:23.510
you might see a strange thing happen.

5
00:23.510 --> 00:29.830
You might see a whole bunch of people in suits who look very much like the financial banker types.

6
00:30.310 --> 00:35.260
At the time when they actually need to pay the bill, you see everybody pull out their business

7
00:35.260 --> 00:37.780
cards and put it into a bowl.

8
00:37.930 --> 00:40.090
So what's actually going on here?

9
00:40.120 --> 00:47.020
A friend of mine told me, apparently there's this game that the rich banker types play where

10
00:47.020 --> 00:50.110
it's kind of like Russian roulette with the bill.

11
00:50.110 --> 00:56.290
So everybody puts their business card in, and the person's card who gets picked out has to pay for

12
00:56.290 --> 00:59.440
everybody's bill, which is kind of crazy,

13
00:59.440 --> 01:01.150
but then again, it's finance. [Music sound by Megan Boni plays]

14
01:01.240 --> 01:04.990
"...finance with a trust fund, 6'5”, blue eyes.” [Music ends]

15
01:04.990 --> 01:05.740
But you know what?

16
01:05.740 --> 01:11.140
We don't need anybody in finance because we are programmers and we can do everything they can do just

17
01:11.140 --> 01:12.130
by writing code.

18
01:12.130 --> 01:13.840
So here's the challenge,

19
01:13.840 --> 01:21.630
you are to look through this friends list and you're going to write some code using what you've learned

20
01:21.630 --> 01:30.060
about randomization in Python, and also lists in Python to be able to print out a random, one of these

21
01:30.060 --> 01:30.870
names.

22
01:30.870 --> 01:35.610
So each time you run the code, it will print out a different name.

23
01:35.610 --> 01:43.470
So if I run the solution file, you can see that each time it prints out a different random name from

24
01:43.470 --> 01:45.710
this list of friends.

25
01:46.340 --> 01:49.970
So think about how you might solve this challenge.

26
01:49.970 --> 01:57.710
You can look through the list of hints in the description box here, but think back to what you've learned

27
01:57.710 --> 02:01.790
in today's lessons and see if you can solve this challenge.

28
02:01.820 --> 02:03.020
Pause the video now.

29
02:05.240 --> 02:05.840
All right.

30
02:05.840 --> 02:14.230
So what we want to do is to be able to tap into this list and get hold of a random item.

31
02:14.350 --> 02:16.990
Now there's two ways of doing this.

32
02:16.990 --> 02:20.290
And it depends on which path you went down.

33
02:20.290 --> 02:27.670
So let's go through the first way, which is maybe you thought let's go through the documentation.

34
02:28.300 --> 02:36.430
So if you look through the random documentation which we saw earlier in today's lessons, you might

35
02:36.430 --> 02:42.130
scroll through all of this and think, hmm, there must be a way that Python has created for us to be

36
02:42.130 --> 02:45.880
able to randomly get items from lists.

37
02:45.910 --> 02:49.150
Now, it's not easy to find, but there is one,

38
02:49.150 --> 02:51.340
it's random.choice() .

39
02:51.370 --> 02:59.820
Now, another way you might have arrived at this is simply using the Google search and saying, "get

40
02:59.820 --> 03:04.980
random item from list..." and then adding the word "Python" at the end.

41
03:05.100 --> 03:10.740
And you might look through StackOverflow and you might come across the same function.

42
03:10.740 --> 03:15.600
Then you can go in the documentation and see how it works and to read about it.

43
03:15.600 --> 03:18.630
So this is probably the simplest way to do it.

44
03:18.630 --> 03:28.280
And we can simply print(random...) Now, in order to use this module, you can see we need to use the import

45
03:28.280 --> 03:29.180
statement,

46
03:29.180 --> 03:31.850
otherwise it doesn't know what random is.

47
03:31.850 --> 03:34.610
So let's import random first,

48
03:34.610 --> 03:36.350
and you can see that error goes away.

49
03:36.350 --> 03:41.720
And then within the Random Module, I'm going to use the choice() function.

50
03:41.720 --> 03:46.220
And this function takes as its input a sequence.

51
03:46.220 --> 03:53.320
Now the list is one such type of sequences in Python, and there are others which we will learn about

52
03:53.320 --> 03:54.700
in later lessons.

53
03:54.700 --> 04:02.350
But this is all that it takes to go ahead and pick out a random name from this list.

54
04:02.380 --> 04:06.190
Now this is option number one,

55
04:07.810 --> 04:14.680
and this is something that you might have thought of based on, you know, thinking about

56
04:14.680 --> 04:18.430
documentation and thinking about how we can find out things from the internet,

57
04:18.430 --> 04:21.040
and this is a really, really key skill of programmers.

58
04:21.040 --> 04:27.340
But the other key skill is figuring out how to do something based on what we already know how to do.

59
04:27.370 --> 04:36.730
So the second option is to use what we already know, which is we know that we can use random.randint()

60
04:36.730 --> 04:42.270
to get a random integer, and we can set the range of random numbers.

61
04:42.270 --> 04:45.330
So we know that lists start counting from 0,

62
04:45.330 --> 04:47.820
so Alice is at index 0.

63
04:47.820 --> 04:54.420
So we would need random numbers between 0, 1, 2, 3, and 4.

64
04:54.420 --> 05:02.310
So if we do randint(0, 4), then we will get a random number anywhere between 0 and 4 including 0

65
05:02.310 --> 05:03.020
and 4.

66
05:03.050 --> 05:12.860
Now if this is our random_index, then we can use this random_index to print out a random name from

67
05:12.860 --> 05:13.700
the friends list.

68
05:13.700 --> 05:20.690
So remember we can tap into a list, add a set of square brackets, and then put in an index

69
05:20.690 --> 05:22.970
to fetch an item from that list.

70
05:22.970 --> 05:30.790
So if I go ahead and run this file, you'll see that the first name comes from the first random.choice(),

71
05:30.790 --> 05:34.420
and the second one comes from using the random_index.

72
05:34.420 --> 05:36.940
And you can see that they're both random,

73
05:36.940 --> 05:41.110
and they both go through our list, and they both do exactly the same function.

74
05:41.110 --> 05:47.200
And if you picked option one or if you picked option two, it's totally the same.

75
05:47.200 --> 05:50.830
It's up to you how you decided to complete this challenge,

76
05:50.830 --> 05:53.440
both ways are equally valid.

77
05:53.470 --> 05:59.080
Now, in reality, if you know that there is something called random.choice(), you would probably go

78
05:59.080 --> 06:03.160
for this because it's less code and it's pretty readable,

79
06:03.160 --> 06:05.890
random.choice() from this list,

80
06:05.890 --> 06:09.220
but the second option is also equally valid.

81
06:09.220 --> 06:14.740
So if you didn't manage to complete the challenge, or if you had any bugs in your code, go back and

82
06:14.740 --> 06:15.190
fix that.

83
06:15.400 --> 06:17.920
Otherwise, I'll see you in the next lesson.