All language subtitles for 042 [Interactive Coding Exercise] Banker Roulette - Who will pay the bill_.en_US

af Afrikaans
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bn Bengali
bs Bosnian
bg Bulgarian
ca Catalan
ceb Cebuano
ny Chichewa
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
tl Filipino
fi Finnish
fr French Download
fy Frisian
gl Galician
ka Georgian
de German
el Greek
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
km Khmer
ko Korean
ku Kurdish (Kurmanji)
ky Kyrgyz
lo Lao
la Latin
lv Latvian
lt Lithuanian
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mn Mongolian
my Myanmar (Burmese)
ne Nepali
no Norwegian
ps Pashto
fa Persian
pl Polish
pt Portuguese
pa Punjabi
ro Romanian
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
st Sesotho
sn Shona
sd Sindhi
si Sinhala
sk Slovak
sl Slovenian
so Somali
es Spanish
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
te Telugu
th Thai
tr Turkish
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu
or Odia (Oriya)
rw Kinyarwanda
tk Turkmen
tt Tatar
ug Uyghur
Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated: 1 00:00:00,450 --> 00:00:07,110 So the code challenge that you're about to do comes from a really interesting phenomenon that you might find 2 00:00:07,110 --> 00:00:07,980 in London. 3 00:00:08,430 --> 00:00:13,700 I live in London and it's well known as a big financial district. 4 00:00:14,220 --> 00:00:21,150 So when you head over to the city or the financial area in London and you go into a restaurant, you 5 00:00:21,150 --> 00:00:23,460 might see a strange thing happen. 6 00:00:23,820 --> 00:00:31,290 You might see a whole bunch of people in suits who look very much like the financial banker types. At 7 00:00:31,290 --> 00:00:33,480 the time when they actually need to pay the bill 8 00:00:33,660 --> 00:00:37,590 you see everybody pull out their business cards and put them into a bowl. 9 00:00:38,310 --> 00:00:39,940 So what's actually going on here? 10 00:00:40,290 --> 00:00:47,550 A friend of mine told me apparently there's this game that the rich banker types play where it's kind 11 00:00:47,550 --> 00:00:50,060 of like Russian roulette with the bill. 12 00:00:50,490 --> 00:00:57,060 So everybody puts their business card in and the person's card who gets picked out has to pay for everybody's 13 00:00:57,060 --> 00:00:59,550 bill, which is kind of crazy. 14 00:00:59,550 --> 00:01:01,240 But then again, it's finance. 15 00:01:01,920 --> 00:01:07,740 So in today's challenge, this is what we're going to replicate with code. Head over to the course 16 00:01:07,740 --> 00:01:11,820 resources and click on the coding exercise to bring it up. 17 00:01:12,540 --> 00:01:17,850 Now, go ahead and fork your own copy of it so that you can make amendments. 18 00:01:18,540 --> 00:01:24,660 Now, the idea is that once you've completed the challenge, you should be able to type in a whole bunch 19 00:01:24,660 --> 00:01:29,910 of names as an input with a comma and a space separating each name. 20 00:01:30,390 --> 00:01:37,350 And then the code will pick a random name from the one that you've given it and tell you who is going 21 00:01:37,350 --> 00:01:38,070 to buy the meal. 22 00:01:40,310 --> 00:01:43,880 Now, there's two lines of code here that you might not have seen before. 23 00:01:45,320 --> 00:01:54,410 Now this is something called split, and this allows you to split a string into separate components 24 00:01:54,710 --> 00:01:56,950 based on some sort of divider. 25 00:01:57,440 --> 00:02:03,360 So in this case, notice how it says hello, from, AskPython. 26 00:02:03,840 --> 00:02:11,900 Now, if we use the split on this string, then it will divide it up into a list and separate out all 27 00:02:11,900 --> 00:02:17,660 the words that are divided by this split character, which is the comma. 28 00:02:18,470 --> 00:02:25,790 Now, similarly, over here, when we give the input, it's going to be in the format like this; everybody's 29 00:02:25,790 --> 00:02:28,190 names separated by a comma and a space. 30 00:02:28,760 --> 00:02:37,970 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 00:02:37,970 --> 00:02:43,160 it will put everything else as separate items inside a list. 32 00:02:44,390 --> 00:02:50,840 So what I recommend doing before you get started writing the actual code is to test it out. 33 00:02:50,840 --> 00:02:53,960 So printout what names actually looks like. 34 00:02:54,380 --> 00:03:01,390 So if we go ahead and run this code and then we type a bunch of names separated by a comma. 35 00:03:01,400 --> 00:03:09,140 so Angela, Ben, Jenny and then hit enter, 36 00:03:09,650 --> 00:03:12,110 then we hit the line where it prints names. 37 00:03:12,530 --> 00:03:18,350 Now names, as you'll see, is now a list with all the names I typed in. 38 00:03:19,010 --> 00:03:25,430 And while previously they were separated by a comma and a space, they are now all individual items 39 00:03:25,430 --> 00:03:26,090 in a list. 40 00:03:26,600 --> 00:03:30,110 The idea is that you've got all of these items inside a list, 41 00:03:30,420 --> 00:03:38,450 how can I pick a random one out of it using a random index based on the number of items in the list 42 00:03:38,480 --> 00:03:39,770 which of course can change? 43 00:03:40,250 --> 00:03:47,300 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 00:03:47,300 --> 00:03:47,630 today? 45 00:03:48,080 --> 00:03:49,440 So have a think about it, 46 00:03:49,820 --> 00:03:52,790 have a pause and then see if you can complete the challenge. 47 00:03:58,680 --> 00:04:05,910 So now that we've got the list as a starting point stored inside names, how can we go about generating 48 00:04:05,940 --> 00:04:08,820 a random name and picking it out of the list? 49 00:04:09,480 --> 00:04:18,210 Well, we know that we can pick a item from the list by adding a set of square brackets and then a index 50 00:04:18,210 --> 00:04:18,630 number. 51 00:04:18,930 --> 00:04:25,080 So in this case, it should print the first item out of the list which is going to be just the word 52 00:04:25,080 --> 00:04:28,420 Angela. And I can run this to prove this to you. 53 00:04:28,800 --> 00:04:35,970 You can see that names is a list of all the names, Angela, James, Ben. Names at position zero 54 00:04:36,120 --> 00:04:38,340 is Angela, the first item. 55 00:04:39,270 --> 00:04:43,830 Now, how can we get a random number to replace that number zero? 56 00:04:44,430 --> 00:04:47,080 Well, we could use our random generator. 57 00:04:47,100 --> 00:04:54,420 Let's go ahead and import the random module and we can say random.randint and then we can specify 58 00:04:54,420 --> 00:04:55,610 the start and end. 59 00:04:56,100 --> 00:05:01,490 So the start is probably going to be zero because that's where we start counting with our lists. 60 00:05:01,890 --> 00:05:03,490 But what is the end? 61 00:05:03,510 --> 00:05:06,450 Well, the end should be the position of the last item. 62 00:05:06,810 --> 00:05:09,180 But how can we know that position? 63 00:05:09,190 --> 00:05:11,250 What is that X going to be? 64 00:05:12,510 --> 00:05:17,600 Well, what if we got hold of the number of items in the list? 65 00:05:18,090 --> 00:05:22,410 So do remember how previously we learned about the len function? 66 00:05:23,070 --> 00:05:30,360 Well, the len function can be used to get the number of elements in a list or the number of characters 67 00:05:30,360 --> 00:05:31,170 in a string. 68 00:05:31,620 --> 00:05:35,970 And if you take a look at this Stack Overflow question, then you'll see it in action. 69 00:05:36,690 --> 00:05:45,060 All that we have to do to get hold of the number of items inside this names list is to write len 70 00:05:46,330 --> 00:05:49,270 (), and then inside it we put names. 71 00:05:49,610 --> 00:05:55,630 Now we can either store this or print this. Llet's go ahead and first print with the value of this is 72 00:05:55,630 --> 00:06:02,760 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 00:06:02,770 --> 00:06:06,450 names separated by a comma, hit enter. 74 00:06:06,760 --> 00:06:10,710 And you can see that this line of code has printed three. 75 00:06:11,110 --> 00:06:14,380 So the length of my names list is three. 76 00:06:14,950 --> 00:06:22,030 So now that we've gotten hold of that, then we can probably use that number that comes from this len 77 00:06:22,030 --> 00:06:31,840 function inside our random generator, because we know that we can use this to get the total number 78 00:06:31,840 --> 00:06:33,800 of items in list. 79 00:06:34,690 --> 00:06:38,370 So in this case, that would be one, two, three, four, five. 80 00:06:38,860 --> 00:06:42,550 This last item is not at position five, though, right? 81 00:06:42,670 --> 00:06:44,200 It's at position zero, 82 00:06:44,200 --> 00:06:46,520 one, two, three, four. 83 00:06:46,870 --> 00:06:51,040 So we always need one less than the total number of items. 84 00:06:51,550 --> 00:06:56,800 And the range of random numbers we would want is num_items -1. 85 00:06:58,230 --> 00:07:11,340 Now, this line of code will allow us to generate random numbers between zero and the last index, and 86 00:07:11,340 --> 00:07:19,500 I can store that inside a random choice variable and print my random choice. 87 00:07:20,990 --> 00:07:22,520 So if I run the code again 88 00:07:23,850 --> 00:07:26,970 and it prints one this time, 89 00:07:28,380 --> 00:07:36,990 but the next time, it might be zero or two, because those are all the possible choices for a three 90 00:07:36,990 --> 00:07:45,930 item list. Now we can use that number to actually get hold of a particular item in our list of names. 91 00:07:46,380 --> 00:07:52,500 So we can say names[], and then we're going to use the random choice to get hold of 92 00:07:52,500 --> 00:08:01,290 the random item. And then we can go ahead and save this as the person_who_will_pay. 93 00:08:02,220 --> 00:08:03,840 And we can print 94 00:08:04,930 --> 00:08:11,890 person_who_will_pay + "is going to buy the meal today." 95 00:08:13,160 --> 00:08:19,610 So now let's go ahead and run our code again and then give everybody's names separated by a comma, and 96 00:08:19,610 --> 00:08:21,890 then it says James is going to buy the meal today. 97 00:08:22,520 --> 00:08:25,430 And now we know that we've completed the challenge. 98 00:08:26,340 --> 00:08:31,490 Now, you'll notice that in the instructions, I told you that you're not allowed to use the choice 99 00:08:31,490 --> 00:08:32,030 function. 100 00:08:32,510 --> 00:08:39,440 And the reason for this is because if you search AskPython and you search for the random module 101 00:08:41,440 --> 00:08:50,530 and you scroll down, you'll find that you can actually generate random items from a sequence such as 102 00:08:50,530 --> 00:08:58,360 a list by writing random.choice, and it will actually pick an item from that list. 103 00:08:59,370 --> 00:09:05,460 So let me demo what our code would look like instead. Instead of needing all of these lines of code, 104 00:09:05,940 --> 00:09:12,420 all we have to write is random.choice() 105 00:09:12,630 --> 00:09:18,450 and then we put our names list inside. And now it will do exactly the same as before 106 00:09:20,110 --> 00:09:27,340 but with far less code. But this, of course, doesn't test whether if you've understood how indices 107 00:09:27,340 --> 00:09:34,270 work with lists and whether if you're comfortable with the idea that the last index is actually one 108 00:09:34,270 --> 00:09:40,960 less than the total number of items because we start counting from zero. I've made it a little bit 109 00:09:40,960 --> 00:09:46,420 harder than it needed to be just to see how good a grasp you have on lists. 110 00:09:47,230 --> 00:09:49,960 Now, I hope that you managed to complete this challenge. 111 00:09:50,320 --> 00:09:51,490 If not, don't worry. 112 00:09:51,490 --> 00:09:55,920 Go back to it and see if you can replicate the logic that we talked about just now. 113 00:09:56,950 --> 00:10:03,190 And if you really struggle visualizing what these lines of code are doing, then you can always pass it 114 00:10:03,190 --> 00:10:08,710 through Thonny and use the debugger to step through it one step at a time and see what's happening at 115 00:10:08,710 --> 00:10:09,350 each stage. 116 00:10:10,000 --> 00:10:16,270 Now on the next lesson, we're going to see some more advanced parts of lists in action, namely how 117 00:10:16,270 --> 00:10:18,560 to nest lists inside other lists. 118 00:10:19,000 --> 00:10:21,970 So for all of that and more, I'll see you on the next lesson. 11806

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.