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
All right guys, I've got a fun coding challenge for you today,
2
and it is a problem called fizzbuzz.
3
Now this is probably one of the most asked questions at interviews for
4
programmers because it tests the way that you think,
5
and it allows the interviewer to see how you use your logic to solve a problem.
6
And the problem we're going to tackle is a children's game called
7
Fizzbuzz. Last time I checked the programmatic solution was actually up on
8
Wikipedia.
9
So I recommend reading the Wikipedia article only after you've solved this
10
Programming challenge. The rules of fizzbuzz are really simple.
11
Basically, you have a bunch of kids who sit around in a circle and going
12
clockwise. The first kid says 1, the second kid says 2.
13
But then when a kid encounters a number that's fully divisible by three,
14
then instead of saying that number, he's going to say Fizz.
15
And when the number is divisible by five,
16
instead of saying the number, they should say Buzz.
17
And if the number is divisible by both three and five, for example, 15,
18
then they should say Fizzbuzz.
19
So your program is going to replicate this for all the numbers up to and
20
including a hundred. To start off, it will probably look something like this.
21
It's going to print one, and then on the next line
22
it will print two and then fizz,
23
because three is divisible by three. And then four,
24
and then five is divisible by five so it's a buzz.
25
And then six is fizz because it's divisible by three and so on and so forth.
26
This is what your program has to do. Take a look at the instructions,
27
make sure that you understand what it is that your program has to do
28
and then pause the video and try to give this a go.
29
All right. So the first thing we have to do is to create a range, right?
30
So let's say that for number in range, and our range is between 1 and 100,
31
so we have to include 101 in here.
32
And what we want to do is we want to check whether if the number's divisible by
33
three, divisible by five,
34
or if it's divisible by both three and five.
35
Now we could write our code by doing this in order, right? We could say
36
if number % 3 is equal to zero,
37
then it's divisible
38
by three.
39
So you might think that you could write the code like this,
40
but there is a crucial problem in the logic here
41
namely that we know that the if, elif, else statements will stop
42
once it's found one statement that's true. For example,
43
if we take the number to be 15,
44
15 divided by three is equal to zero
45
so it's going to carry out whatever instruction is inside this if statement
46
and then it's going to be done with it. It's going to skip this one
47
and crucially, it's going to skip this one
48
which is what we really need. Instead of structuring our code like this,
49
what you might have done and what you should've done is in fact,
50
have this at the top.
51
So first check if it's divisible by three and by five,
52
and then if that is not true,
53
then check to see if it's divisible by five and then divisible by three or the
54
other option works as well.
55
You could do it like this. Because these two have no overlap in terms of the
56
results and then it doesn't really matter.
57
So now we can finally write our code because we can say, well,
58
if the number that we're currently encountering is divisible by both three and
59
five, then we have to print Fizzbuzz.
60
If it's only divisible by three, then we're going to print fizz.
61
And if it's only divisible by five, then we're going to print buzz.
62
All right.
63
Now that still leaves the remaining numbers, right?
64
Because the remaining numbers
65
which don't fit any of the criteria should just be printed out as the number.
66
We can catch that quite easily with an else statement that catches basically every
67
other number, in which case, we're just going to print the number.
68
So now, if I go ahead and run this code,
69
then you can see that all the way from 1 to 100,
70
it's been able to work out which ones are fizz, which ones are buzz,
71
and which ones are fizzbuzz. How did you get on with it?
72
If you got stuck or if you don't understand why this logic matters,
73
then I want you to try both versions that I showed you and see it
74
step-by-step using the Thonny program just as we did before.
75
That way you can visualize what's actually happening in terms of the logic and
76
how it's flowing from one step to the next, to the next. Now,
77
once you've done that, then go back to your exercise and try to complete it.
78
It's really, really important that you don't just say, "Oh yeah, I understand
79
now." It's a really good idea to make sure that your code actually works at the
80
end of the day and to write the code yourself. Today
81
we've learned all about loops and we're going to apply it in our final project.
82
So that's what's waiting up for you in the next lesson.
83
So head over that once you're ready.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.