patternpythonMinor
Finding the multiple of 3 and 5
Viewed 0 times
andthemultiplefinding
Problem
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?
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))Solution
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
Which makes your code reusable for later. Eg you can call functions from your file in other programs.
Your
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
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
As a last point the
Which combines the two exit points into a single one. To sumarize
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
You can even shorten this into
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
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 writtenelif num % 5==0:
return 'Buzz'
return numAlternatives
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
stringdef 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 numThe 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 belowdef fizz_buzz(num):
string = ''
if num % 3 == 0:
string += 'Fizz'
if num % 5==0:
string += 'Buzz'
if string:
return string
return numAs a last point the
return statement could be written using a terniary conditional operator return string if string else nWhich 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 codefor 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;"*100Using 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
Code Snippets
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))elif num % 5==0:
return 'Buzz'
return numdef 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 numdef fizz_buzz(num):
string = ''
if num % 3 == 0:
string += 'Fizz'
if num % 5==0:
string += 'Buzz'
if string:
return string
return numreturn string if string else nContext
StackExchange Code Review Q#132074, answer score: 7
Revisions (0)
No revisions yet.