7
\$\begingroup\$

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”.

Is this the best way of doing this?

def fizz_buzz(num):
    if num%3==0 and num%5==0:
        return 'FizzBuzz'

    elif num % 3 == 0:
        return 'Fizz'

    elif num % 5==0:
        return 'Buzz'
    else:
        return num

for n in range(1,100):
    print(fizz_buzz(n))
\$\endgroup\$
2
  • 4
    \$\begingroup\$ for i in range(1,101): print("Fizz"*(i%3==0) + "Buzz"*(i%5==0) or i) \$\endgroup\$
    – syb0rg
    Jun 15, 2016 at 14:04
  • \$\begingroup\$ "Multiple of both 3 and 5" = "Multiple of 15". \$\endgroup\$ Jun 15, 2016 at 14:13

1 Answer 1

7
\$\begingroup\$

General feedback

I will try to go over some points that can be useful to note. Firstly there are several things I like about your code. Firstly it is very readable. Secondly I like that you split your logic. You also split finding the string and printing it. This is good. With this being said there are always things which could and should be improved

Semantics

You should use the if __name__ == "__main__": module in your answer.

def fizz_buzz(num):
    if num%3==0 and num%5==0:
        return 'FizzBuzz'

    elif num % 3 == 0:
        return 'Fizz'

    elif num % 5==0:
        return 'Buzz'
    else:
        return num

if __name__ == "__main__":

    for n in range(1,100):
        print(fizz_buzz(n))

Which makes your code reusable for later. Eg you can call functions from your file in other programs. Your else clause at the end of the code is useless. You could have written

    elif num % 5==0:
        return 'Buzz'
    return num

Alternatives

One problem with your code is that you have multiple exit points. Now this is not something to sweat too hard over, and it is not a goal to always try have a single exit. However it can be easier to debug a code with fewer exit points. This is of course much more relevant in longer and more complex code. Though it is a good thing to always have in mind. One way to do this is to define a new variable string

def fizz_buzz(num):
    string = ''
    if num%3==0 and num%5==0:
        string = 'FizzBuzz'

    elif num % 3 == 0:
        string = 'Fizz'

    elif num % 5==0:
        string = 'Buzz'

    if string:
       return string
    return num

The code now only has two exit points however it can still be improved. One key point is that if a number is divisible by 3 and 5, it is divisible by 15. So we can gradually build the string, like shown below

def fizz_buzz(num):
    string = ''

    if num % 3 == 0:
        string += 'Fizz'

    if num % 5==0:
        string += 'Buzz'

    if string:
       return string
    return num

As a last point the return statement could be written using a terniary conditional operator

return string if string else n

Which combines the two exit points into a single one. To sumarize

def fizz_buzz(num):
    string = ''
    if num % 3==0: string +='Fizz' 
    if num % 5==0: string +='Buzz'
    return string if string else num

if __name__ == "__main__":

    for n in range(1, 100):
        print(fizz_buzz(n))

Closing comments

Python has a style guide PEP 8 which explains in excruciating detail how to structure your code. I whole heartily recommend skimming through it and follow it.

The problem FizzBuzz is very, very simple. It can be solved in a number of ways using just a simple line. Syb0rg, showed one way to write this code

for i in range(1,101): print("Fizz"*(i%3==0) + "Buzz"*(i%5==0) or i)

You can even shorten this into

i=0;exec"print i%3/2*'Fizz'+i%5/4*'Buzz'or-~i;i+=1;"*100 

Using some cryptic pythonic voodoo. However as I said in the introductory I like your code, because it is easy to understand. Almost always it is better to have clear, readable code than cryptic code which is a few lines shorter. This of course disregards any speed improvements and such

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Wow! thank you so much for taking your time and writing out this helpful and thoro explanation. I love people like you that help new programmers like myself and tell them how to improve their style of code, not just the one problem given and help improve their whole career in programming. \$\endgroup\$ Jun 15, 2016 at 15:50

Not the answer you're looking for? Browse other questions tagged or ask your own question.