Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranรฎ)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
1
So the code challenge that you're about to do comes from a really interesting phenomenon that you might find
2
in London.
3
I live in London and it's well known as a big financial district.
4
So when you head over to the city or the financial area in London and you go into a restaurant, you
5
might see a strange thing happen.
6
You might see a whole bunch of people in suits who look very much like the financial banker types. At
7
the time when they actually need to pay the bill
8
you see everybody pull out their business cards and put them into a bowl.
9
So what's actually going on here?
10
A friend of mine told me apparently there's this game that the rich banker types play where it's kind
11
of like Russian roulette with the bill.
12
So everybody puts their business card in and the person's card who gets picked out has to pay for everybody's
13
bill, which is kind of crazy.
14
But then again, it's finance.
15
So in today's challenge, this is what we're going to replicate with code. Head over to the course
16
resources and click on the coding exercise to bring it up.
17
Now, go ahead and fork your own copy of it so that you can make amendments.
18
Now, the idea is that once you've completed the challenge, you should be able to type in a whole bunch
19
of names as an input with a comma and a space separating each name.
20
And then the code will pick a random name from the one that you've given it and tell you who is going
21
to buy the meal.
22
Now, there's two lines of code here that you might not have seen before.
23
Now this is something called split, and this allows you to split a string into separate components
24
based on some sort of divider.
25
So in this case, notice how it says hello, from, AskPython.
26
Now, if we use the split on this string, then it will divide it up into a list and separate out all
27
the words that are divided by this split character, which is the comma.
28
Now, similarly, over here, when we give the input, it's going to be in the format like this; everybody's
29
names separated by a comma and a space.
30
So now if we use the split on a comma and a space, then it will take out the comma and the space and
31
it will put everything else as separate items inside a list.
32
So what I recommend doing before you get started writing the actual code is to test it out.
33
So printout what names actually looks like.
34
So if we go ahead and run this code and then we type a bunch of names separated by a comma.
35
so Angela, Ben, Jenny and then hit enter,
36
then we hit the line where it prints names.
37
Now names, as you'll see, is now a list with all the names I typed in.
38
And while previously they were separated by a comma and a space, they are now all individual items
39
in a list.
40
The idea is that you've got all of these items inside a list,
41
how can I pick a random one out of it using a random index based on the number of items in the list
42
which of course can change?
43
And then how can I get a random name as the output and then print so-and-so is going to buy the meal
44
today?
45
So have a think about it,
46
have a pause and then see if you can complete the challenge.
47
So now that we've got the list as a starting point stored inside names, how can we go about generating
48
a random name and picking it out of the list?
49
Well, we know that we can pick a item from the list by adding a set of square brackets and then a index
50
number.
51
So in this case, it should print the first item out of the list which is going to be just the word
52
Angela. And I can run this to prove this to you.
53
You can see that names is a list of all the names, Angela, James, Ben. Names at position zero
54
is Angela, the first item.
55
Now, how can we get a random number to replace that number zero?
56
Well, we could use our random generator.
57
Let's go ahead and import the random module and we can say random.randint and then we can specify
58
the start and end.
59
So the start is probably going to be zero because that's where we start counting with our lists.
60
But what is the end?
61
Well, the end should be the position of the last item.
62
But how can we know that position?
63
What is that X going to be?
64
Well, what if we got hold of the number of items in the list?
65
So do remember how previously we learned about the len function?
66
Well, the len function can be used to get the number of elements in a list or the number of characters
67
in a string.
68
And if you take a look at this Stack Overflow question, then you'll see it in action.
69
All that we have to do to get hold of the number of items inside this names list is to write len
70
(), and then inside it we put names.
71
Now we can either store this or print this. Llet's go ahead and first print with the value of this is
72
going to be. Let's go ahead and comment out this line of code and then run our code, and then give everybody's
73
names separated by a comma, hit enter.
74
And you can see that this line of code has printed three.
75
So the length of my names list is three.
76
So now that we've gotten hold of that, then we can probably use that number that comes from this len
77
function inside our random generator, because we know that we can use this to get the total number
78
of items in list.
79
So in this case, that would be one, two, three, four, five.
80
This last item is not at position five, though, right?
81
It's at position zero,
82
one, two, three, four.
83
So we always need one less than the total number of items.
84
And the range of random numbers we would want is num_items -1.
85
Now, this line of code will allow us to generate random numbers between zero and the last index, and
86
I can store that inside a random choice variable and print my random choice.
87
So if I run the code again
88
and it prints one this time,
89
but the next time, it might be zero or two, because those are all the possible choices for a three
90
item list. Now we can use that number to actually get hold of a particular item in our list of names.
91
So we can say names[], and then we're going to use the random choice to get hold of
92
the random item. And then we can go ahead and save this as the person_who_will_pay.
93
And we can print
94
person_who_will_pay + "is going to buy the meal today."
95
So now let's go ahead and run our code again and then give everybody's names separated by a comma, and
96
then it says James is going to buy the meal today.
97
And now we know that we've completed the challenge.
98
Now, you'll notice that in the instructions, I told you that you're not allowed to use the choice
99
function.
100
And the reason for this is because if you search AskPython and you search for the random module
101
and you scroll down, you'll find that you can actually generate random items from a sequence such as
102
a list by writing random.choice, and it will actually pick an item from that list.
103
So let me demo what our code would look like instead. Instead of needing all of these lines of code,
104
all we have to write is random.choice()
105
and then we put our names list inside. And now it will do exactly the same as before
106
but with far less code. But this, of course, doesn't test whether if you've understood how indices
107
work with lists and whether if you're comfortable with the idea that the last index is actually one
108
less than the total number of items because we start counting from zero. I've made it a little bit
109
harder than it needed to be just to see how good a grasp you have on lists.
110
Now, I hope that you managed to complete this challenge.
111
If not, don't worry.
112
Go back to it and see if you can replicate the logic that we talked about just now.
113
And if you really struggle visualizing what these lines of code are doing, then you can always pass it
114
through Thonny and use the debugger to step through it one step at a time and see what's happening at
115
each stage.
116
Now on the next lesson, we're going to see some more advanced parts of lists in action, namely how
117
to nest lists inside other lists.
118
So for all of that and more, I'll see you on the next lesson.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.