One of the great strengths of computer programs is that they can process an incredible amount of information far faster than we could do it manually. To achieve this we need to be able to loop through collections and sequences. In this hands-on lab, we’ll work through the popular interviewing Fizz-Buzz problem by creating a script that will prompt the user for the number of items to process, and then print out either the number, “Fizz”, “Buzz”, or “FizzBuzz” for each number between 1 and the user-provided number.
To feel comfortable completing this lab you’ll want to know how to do the following:
* Utilize `for` loops. Watch “The `for` Loop” video from the Certified Entry-Level Python Programmer Certification course.
* Nest conditionals and loops. Watch the “Nesting Loops and Conditionals” video from the Certified Entry-Level Python Programmer Certification course.
* Utilize the `range` type. Watch “Using `range`” video from the Certified Entry-Level Python Programmer Certification course.
Learning Objectives
Successfully complete this lab by achieving the following learning objectives:
- Create the fizz-buzz.py Script, Make It Executable with python3.7, and Accept User Input
We’re going to create
fizz-buzz.py
, placing it in our home directory (~
), and we want to make sure that we can run it directly. To keep from being completely tied to the path of ourpython3.7
binary, we want to make sure that our shebang is set up properly.Let’s create the file and set the shebang:
~/fizz-buzz.py
#!/usr/bin/env python3.7 # Python implementation here
With the file created, we need to also make sure that it’s executable, and we can do this using
chmod
:$ chmod u+x ~/fizz-buzz.py
Next, we’ll prompt the user for a value, convert it to an
int
, and store it off in a variable.~/fizz-buzz.py
#!/usr/bin/env python3.7 upper_number = int(input("How many values should we process: "))
- Create a Range from 1 to the User’s Provided Number and Loop Through It
To print the number from
1
to the user’s provided number (including that number), we’ll want to utilize therange
type and afor
loop. Here’s what our loop will look like:~/fizz-buzz.py
#!/usr/bin/env python3.7 upper_number = int(input("How many values should we process: ")) for number in range(1, upper_number + 1): print(number)
If we run it again, and enter 10 at the prompt, we’ll see the numbers 1-10 printed out to the terminal. For now, replace
print(number)
withpass
, and we can move on to write the conditional logic to process each number.- Print “FizzBuzz” if the Value Is a Multiple of Three and Five
Now that we have a
number
value to use, we can create our conditional. Since we have a case that should trigger if the value is a multiple of three and five, then we’ll want that branch to be before the only multiple of three or only multiple of five branches, because either of those would also be true. Let’s create ourif
statement and print "FizzBuzz" if the condition is met, then add anelse
to just print the value otherwise:~/fizz-buzz.py
#!/usr/bin/env python3.7 upper_number = int(input("How many values should we process: ")) for number in range(1, upper_number + 1): if number % 3 == 0 and number % 5 == 0: print("FizzBuzz") else: print(number)
Now we can run the script to make sure that what we’ve written up to this point is working properly:
$ ./fizz-buzz.py How many values should we process: 20 ... 12 13 14 FizzBuzz 16 ... $
15 is replaced by FizzBuzz, because it’s evenly divisible by 3 and 5, and the rest of the numbers print out normally.
- Print “Fizz” if the Value Is a Multiple of Three and “Buzz” if It’s a Multiple of Five
To handle the other two cases of the Fizz-Buzz problem, we’ll need to utilize
elif
statements for each of the individual comparisons that we used in ourif
statement. Let’s add those now:~/fizz-buzz.py
#!/usr/bin/env python3.7 upper_number = int(input("How many values should we process: ")) for number in range(1, upper_number + 1): if number % 3 == 0 and number % 5 == 0: print("FizzBuzz") elif number % 3 == 0: print("Fizz") elif number % 5 == 0: print("Buzz") else: print(number)
Now we can run the script to make sure that what we’ve written up to this point is working properly:
$ ./fizz-buzz.py How many values should we process: 30 1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz 16 17 ... $