In this exercise,
you are going to write a program
that is going to select a random name from a list of names.
And the person selected will have to pay
for everyone's food bill.
This might be handy when you're going out for dinner
with your friends, or maybe when you're going out
with your colleagues and you want to keep things simple.
So this is a Banker Roulette and the idea
is we're going to put a bunch of names
into the Input area,
and these names will be separated by a comma and a space.
This is very important.
On line one, we've got some code that is going
to do some pre-processing.
It's going to take the input and it's going to split
that string by a comma and a space.
And then it's going to add each of those names into a list,
which you can then access in that name's variable.
Now don't worry about getting hold of the Input,
we've already sorted that out behind the scenes for you,
all you have to do is given a list of names,
how can you pick out a random one from those names
and then print out that name and then the phrase,
"is going to buy the meal today."
So take a look inside the Example Input
and Example Output and figure out how you
can get this to work.
Once you're done, hit Submit and then test your code
against our pre-formatted test cases.
Now remember, your output can only have one line,
and it must match the format written in the example output.
All right, let's work through the solution together.
The first thing we're going to do is to get hold
of the total number of items in the list.
So we're creating a variable called num_items,
and we're using the len() function in Python to calculate
how many items there are in our list,
that is the list of names.
Next, we are taking the number of items,
we're subtracting it by 1
because remember, lists start from 0
and then to the last index.
So if a list had three items,
then it would be at positions 0, 1, and 2.
So that's why we're subtracting number_items by 1.
And then we're using random.randint()
in order to get a random number between 0
and the last index.
And then that is going to be the number
we're going use to pick out a random name in the next step.
Once we've got our random number,
then it's as simple as taking our list of names,
putting a square bracket [ ] afterwards
and then putting our random choice,
that random number into the square brackets
to pick out a random name.
And finally, we put that name into our print statement
and print out that person is going to buy the meal today.