So the code challenge that you're about to do comes from a really interesting phenomenon that you might
find in London.
I live in London and it's well known as a big financial district.
So when you head over to the city or the financial area in London and you go into a restaurant,
you might see a strange thing happen.
You might see a whole bunch of people in suits who look very much like the financial banker types.
At the time when they actually need to pay the bill, you see everybody pull out their business
cards and put it into a bowl.
So what's actually going on here?
A friend of mine told me, apparently there's this game that the rich banker types play where
it's kind of like Russian roulette with the bill.
So everybody puts their business card in, and the person's card who gets picked out has to pay for
everybody's bill, which is kind of crazy,
but then again, it's finance. [Music sound by Megan Boni plays]
"...finance with a trust fund, 6'5”, blue eyes.” [Music ends]
But you know what?
We don't need anybody in finance because we are programmers and we can do everything they can do just
by writing code.
So here's the challenge,
you are to look through this friends list and you're going to write some code using what you've learned
about randomization in Python, and also lists in Python to be able to print out a random, one of these
names.
So each time you run the code, it will print out a different name.
So if I run the solution file, you can see that each time it prints out a different random name from
this list of friends.
So think about how you might solve this challenge.
You can look through the list of hints in the description box here, but think back to what you've learned
in today's lessons and see if you can solve this challenge.
Pause the video now.
All right.
So what we want to do is to be able to tap into this list and get hold of a random item.
Now there's two ways of doing this.
And it depends on which path you went down.
So let's go through the first way, which is maybe you thought let's go through the documentation.
So if you look through the random documentation which we saw earlier in today's lessons, you might
scroll through all of this and think, hmm, there must be a way that Python has created for us to be
able to randomly get items from lists.
Now, it's not easy to find, but there is one,
it's random.choice() .
Now, another way you might have arrived at this is simply using the Google search and saying, "get
random item from list..." and then adding the word "Python" at the end.
And you might look through StackOverflow and you might come across the same function.
Then you can go in the documentation and see how it works and to read about it.
So this is probably the simplest way to do it.
And we can simply print(random...) Now, in order to use this module, you can see we need to use the import
statement,
otherwise it doesn't know what random is.
So let's import random first,
and you can see that error goes away.
And then within the Random Module, I'm going to use the choice() function.
And this function takes as its input a sequence.
Now the list is one such type of sequences in Python, and there are others which we will learn about
in later lessons.
But this is all that it takes to go ahead and pick out a random name from this list.
Now this is option number one,
and this is something that you might have thought of based on, you know, thinking about
documentation and thinking about how we can find out things from the internet,
and this is a really, really key skill of programmers.
But the other key skill is figuring out how to do something based on what we already know how to do.
So the second option is to use what we already know, which is we know that we can use random.randint()
to get a random integer, and we can set the range of random numbers.
So we know that lists start counting from 0,
so Alice is at index 0.
So we would need random numbers between 0, 1, 2, 3, and 4.
So if we do randint(0, 4), then we will get a random number anywhere between 0 and 4 including 0
and 4.
Now if this is our random_index, then we can use this random_index to print out a random name from
the friends list.
So remember we can tap into a list, add a set of square brackets, and then put in an index
to fetch an item from that list.
So if I go ahead and run this file, you'll see that the first name comes from the first random.choice(),
and the second one comes from using the random_index.
And you can see that they're both random,
and they both go through our list, and they both do exactly the same function.
And if you picked option one or if you picked option two, it's totally the same.
It's up to you how you decided to complete this challenge,
both ways are equally valid.
Now, in reality, if you know that there is something called random.choice(), you would probably go
for this because it's less code and it's pretty readable,
random.choice() from this list,
but the second option is also equally valid.
So if you didn't manage to complete the challenge, or if you had any bugs in your code, go back and
fix that.
Otherwise, I'll see you in the next lesson.