Locked lesson.
About this lesson
Exercise files
Download this lesson’s related exercise files.
FizzBuzz!.docx60.3 KB FizzBuzz! - Solution.docx
59.2 KB
Quick reference
FizzBuzz!
FizzBuzz is a simple test of your basic python skills.
When to use
FizzBuzz is a historic interview question to test your coding knowledge.
Instructions
Print out all the numbers from 1 to 100. If a number is divisible by 3, print fizz, if it's divisible by 5, print buzz; if it's divisible by 3 and 5, print fizzbuzz - otherwise just print the number.
count = 0
while (count < 100):
count+=1
if (count % 3 == 0) and (count % 5 == 0):
print "%s FIZZBUZZ!" % count
elif (count % 3 == 0):
print "%s FIZZ!" % count
elif (count % 5 == 0):
print "%s BUZZ!" % count
else:
print(count)
Hints & tips
- Fizzbuzz is an old interview question
- It tests your python knowledge!
- 00:04 In this video I want to take just a quick step back and sort of take all the things
- 00:08 we've learned so far and create this fun little FizzBuzz app.
- 00:12 FizzBuzz is sort of a traditional interview question.
- 00:15 You go in to get a coding job, they're going to ask you to do Fizz Buzz.
- 00:18 Well, not anymore, it use to be a very popular interviewing question, nowadays,
- 00:22 they do other interview questions because everybody knows Fizz Buzz, but
- 00:25 it's still a fun little thing to look at, that's what we're going to do.
- 00:28 So the idea of Fizz Buzz is you're told to print out all the numbers between 1 and
- 00:32 100 onto the screen.
- 00:34 And if a number is divisible by three, print out the word fizz if it's divisible
- 00:38 by five, print out the word buzz, if it's divisible by both three and
- 00:42 five print out fizz buzz, otherwise just print out the number.
- 00:45 If you think about it that sort of uses all the things we've learned so far loops,
- 00:49 if statements, else statements, eliff statements,
- 00:52 we can determine if a number is divisible by another number by using the modulus.
- 00:56 If a number is divisible It won't have a remainder, so the modules will be zero.
- 01:01 So we can do an if statement to test for that and find that out.
- 01:04 take just a minute to pause this video and sort of think through it yourself.
- 01:08 How would you do it, try and see if you can do it yourself because that's a fun
- 01:11 little test to give yourself to see if you can do it.
- 01:14 First things first, we're going to need a loop.
- 01:15 You can use any kind of loop you want, I kind of like while loop, so
- 01:18 I'm going to use a while loop.
- 01:19 So let's create our counter, let's call it count set it equal to 0,
- 01:24 then let's go while count is less than 100, and let's just print out our count.
- 01:30 And then of course, we have to be sure to increment our counter.
- 01:35 So then I'm getting an infinite loop, so
- 01:38 let's just run this 0 to 100, or up to 0 to 99.
- 01:41 Let's change this like this, run it again, I like to make little changes and
- 01:46 then run my code to see if there's any error.
- 01:49 Right now we saw, okay, now it's 100 by moving our account up,
- 01:53 so that's a good thing to do.
- 01:54 Get rid of that.
- 01:55 Now, let's do our if stuff.
- 01:56 First off, let's try and determine if a number is divisible by both 3 and 5.
- 02:00 So if count, modulus 3 = 0 and
- 02:07 count modulus 5 = 0.
- 02:13 Print and let's print out the number itself.
- 02:18 And then the word FizzBuzz and we've seen this before
- 02:23 we can do this percent sign to swap this in, okay?
- 02:28 So that looks good now elif, now, lets go and
- 02:31 do each one individually, so elif count modulus 3 = 0.
- 02:36 And I'm just going to copy this line and change this to FIZZ,
- 02:43 elif count modulus 5 = 0, don't forget our colons.
- 02:49 And you can wrap these guys like this if you want.
- 02:53 You don't necessarily have to sort of a matter of style.
- 02:56 Again, so if it's divisible by five we want BUZZ.
- 02:59 Okay, so far so good, and then finally else,
- 03:03 if it's not divisible by any of them let's just print out the number itself.
- 03:08 I think that's about it.
- 03:09 We just save this, now, let's run it and see how well we did.
- 03:12 Okay, so let's scroll back down and start at the beginning, 1, 2, 3,
- 03:17 3 is divisible by 3, so it prints out FIZZ!, so far so good.
- 03:21 4, 5 is divisible by 5, so it prints out BUZZ!,
- 03:24 we get another FIZZ!, another FIZZ!, a BUZZ!, FIZZ!.
- 03:29 Now, this is our first one where its both 15 is divisible by 3 and 5, so FIZZBUZZ!.
- 03:34 We can kind of eyeball this down to 30 is the next
- 03:40 one FIZZBUZZ!, FIZZBUZZ!, looking good.
- 03:45 I'm going to get to 100 BUZZ!, FIZZ!, and so there we go just like this.
- 03:47 Pretty simple but
- 03:48 you got to sort of think it through at least the first time you do it.
- 03:50 And it is a little tricky I like to start out with this testing for
- 03:53 both of them right at the beginning sort of knock that right out,
- 03:56 and then using elifs for the rest of them.
- 03:58 But think about yourself, think about trying to find a different way to do it,
- 04:02 try it with a for loop.
- 04:03 Can you do it with a for loop?
- 04:04 Try and do it with a while statement in a different way.
- 04:06 Can you do it with less code?
- 04:08 Can you do it with more code?
- 04:09 Just play around with it.
- 04:11 It's a nice little tool to test yourself and
- 04:13 sort of sharpen your skills that you've learned so far, so that's FIZZBUZZ.
- 04:17 In the next video, we're going to jump into functions.
Lesson notes are only available for subscribers.