1
00:00:00,540 --> 00:00:01,589
In this exercise,

2
00:00:01,589 --> 00:00:03,570
you are going to write a program

3
00:00:03,570 --> 00:00:08,570
that is going to select a random name from a list of names.

4
00:00:09,090 --> 00:00:11,040
And the person selected will have to pay

5
00:00:11,040 --> 00:00:12,840
for everyone's food bill.

6
00:00:12,840 --> 00:00:14,910
This might be handy when you're going out for dinner

7
00:00:14,910 --> 00:00:17,220
with your friends, or maybe when you're going out

8
00:00:17,220 --> 00:00:20,670
with your colleagues and you want to keep things simple.

9
00:00:20,670 --> 00:00:24,660
So this is a Banker Roulette and the idea

10
00:00:24,660 --> 00:00:27,120
is we're going to put a bunch of names

11
00:00:27,120 --> 00:00:29,520
into the Input area,

12
00:00:29,520 --> 00:00:34,230
and these names will be separated by a comma and a space.

13
00:00:34,230 --> 00:00:36,006
This is very important.

14
00:00:36,006 --> 00:00:39,840
On line one, we've got some code that is going

15
00:00:39,840 --> 00:00:42,000
to do some pre-processing.

16
00:00:42,000 --> 00:00:45,780
It's going to take the input and it's going to split

17
00:00:45,780 --> 00:00:49,260
that string by a comma and a space.

18
00:00:49,260 --> 00:00:53,220
And then it's going to add each of those names into a list,

19
00:00:53,220 --> 00:00:56,853
which you can then access in that name's variable.

20
00:00:57,990 --> 00:01:00,090
Now don't worry about getting hold of the Input,

21
00:01:00,090 --> 00:01:03,240
we've already sorted that out behind the scenes for you,

22
00:01:03,240 --> 00:01:08,240
all you have to do is given a list of names,

23
00:01:09,420 --> 00:01:13,530
how can you pick out a random one from those names

24
00:01:13,530 --> 00:01:18,300
and then print out that name and then the phrase,

25
00:01:18,300 --> 00:01:20,100
"is going to buy the meal today."

26
00:01:20,100 --> 00:01:22,320
So take a look inside the Example Input

27
00:01:22,320 --> 00:01:24,604
and Example Output and figure out how you

28
00:01:24,604 --> 00:01:26,130
can get this to work.

29
00:01:26,130 --> 00:01:29,160
Once you're done, hit Submit and then test your code

30
00:01:29,160 --> 00:01:32,130
against our pre-formatted test cases.

31
00:01:32,130 --> 00:01:35,100
Now remember, your output can only have one line,

32
00:01:35,100 --> 00:01:38,433
and it must match the format written in the example output.

33
00:01:41,160 --> 00:01:44,130
All right, let's work through the solution together.

34
00:01:44,130 --> 00:01:46,740
The first thing we're going to do is to get hold

35
00:01:46,740 --> 00:01:50,610
of the total number of items in the list.

36
00:01:50,610 --> 00:01:52,910
So we're creating a variable called num_items,

37
00:01:53,910 --> 00:01:58,200
and we're using the len() function in Python to calculate

38
00:01:58,200 --> 00:02:01,230
how many items there are in our list,

39
00:02:01,230 --> 00:02:03,453
that is the list of names.

40
00:02:06,450 --> 00:02:09,360
Next, we are taking the number of items,

41
00:02:09,360 --> 00:02:11,520
we're subtracting it by 1

42
00:02:11,520 --> 00:02:15,600
because remember, lists start from 0

43
00:02:15,600 --> 00:02:18,210
and then to the last index.

44
00:02:18,210 --> 00:02:20,580
So if a list had three items,

45
00:02:20,580 --> 00:02:24,060
then it would be at positions 0, 1, and 2.

46
00:02:24,060 --> 00:02:27,420
So that's why we're subtracting number_items by 1.

47
00:02:27,420 --> 00:02:29,580
And then we're using random.randint()

48
00:02:29,580 --> 00:02:32,430
in order to get a random number between 0

49
00:02:32,430 --> 00:02:34,350
and the last index.

50
00:02:34,350 --> 00:02:36,090
And then that is going to be the number

51
00:02:36,090 --> 00:02:39,570
we're going use to pick out a random name in the next step.

52
00:02:39,570 --> 00:02:41,310
Once we've got our random number,

53
00:02:41,310 --> 00:02:44,820
then it's as simple as taking our list of names,

54
00:02:44,820 --> 00:02:46,950
putting a square bracket [ ] afterwards

55
00:02:46,950 --> 00:02:48,600
and then putting our random choice,

56
00:02:48,600 --> 00:02:51,090
that random number into the square brackets

57
00:02:51,090 --> 00:02:53,700
to pick out a random name.

58
00:02:53,700 --> 00:02:57,450
And finally, we put that name into our print statement

59
00:02:57,450 --> 00:03:01,323
and print out that person is going to buy the meal today.

