-
-
Save franklinchou/b0a9720f393e0ce166dae500ab5918ee to your computer and use it in GitHub Desktop.
""" | |
"Write a program that prints the numbers from 1 to 100. But for multiples of three print "Fizz" instead of | |
the number and for the multiples of five print "Buzz". For numbers which are multiples of both three and five | |
print "FizzBuzz". | |
""" | |
if __name__ == "__main__": | |
for index in range(1, 100 + 1): | |
if (index % 3 == 0): | |
print("Fizz") | |
elif (index % 5 == 0): | |
print("Buzz") | |
elif (index % 3 == 0 and index % 5 == 0): | |
print("FizzBuzz") | |
else: | |
print(index) |
Try entering that code into the Python REPL. (Type it in exactly as you see it.)
The first line of code that begins with if __name__ == "__main__"
is called the "main method." This is where the Python interpreter goes first to find instructions to run.
You can also extract the program into two separate functions like this:
# This is fizzbuzz using the for loop
def fizzbuzz_for():
for index in range(1, 100 + 1):
if (index % 3 == 0):
print("Fizz")
elif (index % 5 == 0):
print("Buzz")
elif (index % 3 == 0 and index % 5 == 0):
print("FizzBuzz")
else:
print(index)
And like this:
# This is fizzbuzz using the while loop
def fizzbuzz_while():
exit_condition = 100
start = 1
index = start
while(index <= exit_condition):
if (index % 3 == 0):
print("Fizz")
elif (index % 5 == 0):
print("Buzz")
elif (index % 3 == 0 and index % 5 == 0):
print("FizzBuzz")
else:
print(index)
index = index + 1
And you can call them from the main
method like this:
if __name__ == "__main__":
fizzbuzz_for()
Try calling the other implementation using the while loop.
An essential part of software engineering is to generalize code. In other words, to write code that performs the most basic operations in the most general form. Code that is general only needs to be written once and can be applied anywhere. You'll notice that both of those fizzbuzz implementations have a lot of repeated code. Especially the part that does the printing.
We can abstract that code like this:
def fizz_buzz(i: int):
if (i % 3 == 0):
print("Fizz")
elif (i % 5 == 0):
print("Buzz")
elif (i % 3 == 0 and i % 5 == 0):
print("FizzBuzz")
else:
print(i)
You'll notice that the new function fizz_buzz
accepts a parameter. A parameter is a value that is passed into the function that allows the function to perform work.
We can abstract all of the workings of the FizzBuzz program like this:
def fizzbuzz_for_abstract():
for index in range(1, 100 + 1):
fizz_buzz(index)
Here the fizzbuzz_for_abstract
calls fizz_buzz
to do all the printing.
Then we can have a fizzbuzz implementation that looks like this:
def fizz_buzz(i: int):
if (i % 3 == 0):
print("Fizz")
elif (i % 5 == 0):
print("Buzz")
elif (i % 3 == 0 and i % 5 == 0):
print("FizzBuzz")
else:
print(i)
def fizzbuzz_for_abstract():
for index in range(1, 100 + 1):
fizz_buzz(index)
if __name__ == "__main__":
fizzbuzz_for_abstract()
Type that into your interpreter and see it run!
You can also write the same program like this: