I have been trying to learn about the FizzBuzz problem using Python, I found it fairly easy so decided to add an extra layer of complexity.
My Question is: How would I also get the program to also print'Fizz' or 'Buzz' for any numbers that also contain the number 3 or 5 aswell.
I'm not sure if i should progress converting this to string then splitting it and comparing strings or if there is a function i'm missing that would solve this issue more efficently
My current code is displayed below:
for fizzBuzz in range(0, 100):
if fizzBuzz % 5 == 0 and fizzBuzz % 3 == 0:
print('FizzBuzz')
elif fizzBuzz % 5 == 0:
print('Buzz')
elif fizzBuzz % 3 == 0:
print('Fizz')
else:
print(fizzBuzz)
One of the simplest and most straightforward ways to do this would be to convert the number to a string.
Solution:
for fizzBuzz in range(0, 100):
three = fizzBuzz % 3 == 0 or "3" in str(fizzBuzz)
five = fizzBuzz % 5 == 0 or "5" in str(fizzBuzz)
if three and five:
print("FizzBuzz")
elif five:
print("Buzz")
elif three:
print("Fizz")
else:
print(fizzBuzz)