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
for i in range(1,101): print("Fizz"*(i%3==0) + "Buzz"*(i%5==0) or i)
\$\endgroup\$