An Examination of Fizzbuzz
Tags: fizzbuzz, python
Published: 2017-08-01 06:59:34

---
title: An Examination of Fizzbuzz
published: true
description: My attempts at the infamous fizzbuzz problem
tags: fizzbuzz,python
---
<div id="table-of-contents">
<h2>Table of Contents</h2>
<div id="text-table-of-contents">
<ul>
<li><a href="#orgad2678a">1. Introduction</a></li>
<li><a href="#org409ad2f">2. First Attempt</a></li>
<li><a href="#org248163d">3. Second Attempt</a></li>
</ul>
</div>
</div>


<a id="orgad2678a"></a>

# Introduction

The fizz buzz problem is a type of programming challenge to show if a person knows how to program.
Essentially the challenge goes like this, you must create a program that will print fizz every multiple of 3, buzz every multiple of 5,
and fizz buzz on multiples of both fizz and buzz.
*EDIT* I forgot to mention that if n is neither a multiple of 3 or 5 it just prints the number.

These are my attempts on this challenge.
This article is ongoing as I am given new things to try.


<a id="org409ad2f"></a>

# First Attempt

```python 
for n in range(1,101):
    cond1 = n % 3 == 0
    cond2 = n % 5 == 0

    if cond1 and not cond2:
        print("fizz")
    elif cond2 and not cond1:
        print("buzz")
    elif cond1 and cond2:
        print("fizz buzz")
    else:
        print(n)

```


So the first try works as needed. However this method does not readly support changes.
I did however make the choice based on conditions, however adding in additional conditions would be a pain.
Also the code executes the moment that you run it, which is not a very good practice in the python land.


<a id="org248163d"></a>

# Second Attempt

```python
import sys
import argparse


def if_multiple(n, multiple, string):
    if n % multiple == 0:
        return string
    else:
        return ''

def n_if_empty(output, n):
    if output == '':
        return str(n)
    else:
        return ''

def fizzbuzz(start=1,
          stop=101):

    for n in range(start,stop):

        output = ''
        output += if_multiple(n,3,"fizz")
        output += if_multiple(n,5,"buzz")
        output += n_if_empty(output, n)
        print(output)

def main():
    parser = argparse.ArgumentParser(description="A program that plays fizzbuzz.")
    parser.add_argument('start', type=int, help="The number to count from (Default 1)", default=1)
    parser.add_argument('stop', type=int, help="The number to count to (Default: 100)", default=100)

    args = parser.parse_args()

    fizzbuzz(args.start, (args.stop + 1))

    return None




if __name__ == '__main__':
    main()
    sys.exit()

```

The second version is better in my opinion. It no longer relies on messy else if logic, and is instead although I did not run this code through autopep8 so it is not necessarily pythonic. However this version is rather neat. You can import the fizzbuzz function if need be, and you can also import the other functions. Adding in a new bit of logic is as simple as adding in a new line. And changing the string to print is as simple as changing a variable.  

*EDIT* I removed the bash calls from the code blocks, I still haven't quite figured out the org markdown export yet. This code should run without any syntax errors.