Fizzbuzz program in Python
In this post, you will learn how to write fizzbuzz program using the Python programming language. Such a type of programming is generally asked in coding interviews.
Fizzbuzz is a gathering word game for children to teach them about division. Players alternate to check gradually, replacing any number divisible by three with "fizz", and divisible by five with "buzz".
Logic of Fizzbuzz
Suppose, we have given a string representation of a number from the range 1 to n. We have to write a program to iterate over the range and display a string representation of all numbers from 1 to n with considering the following steps -
if a number is divisible by 3 and 5 both, print "FizzBuzz"
otherwise when the number is divisible by 3, print "Fizz"
otherwise when the number is divisible by 5, print "Buzz"
otherwise, write the number as a string
Fizzbuzz program in Python using for-in-range loop
In the given Python program, we loop for 50 times using for-in-range loop to solve the fizzbuzz problem.
# Python program to print Fizz Buzz
# Loop for 50 times
for fizzbuzz in range(1,50):
# Numbers which are dividable by 15,
# (divisible by both 3 & 5),
# print 'FizzBuzz' in place of the number
if fizzbuzz % 15 == 0:
print("FizzBuzz")
continue
# Numbers which are dividable by 3,
# print 'Fizz' in place of the number
elif fizzbuzz % 3 == 0:
print("Fizz")
continue
# Numbers which are dividable by 5,
# print 'Buzz' in place of the number
elif fizzbuzz % 5 == 0:
print("Buzz")
continue
# Print numbers
print(fizzbuzz)
Output of the above code:
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
16
17
Fizz
19
Buzz
Fizz
22
23
Fizz
Buzz
26
Fizz
28
29
FizzBuzz
31
32
Fizz
34
Buzz
Fizz
37
38
Fizz
Buzz
41
Fizz
43
44
FizzBuzz
46
47
Fizz
49
Fizzbuzz program in Python using function
Here, we define a function fizzBuzz() to solve the fizzbuzz problem in Python. We use a for-in-range loop to traverse numbers from 1 to n.
class FizzBuzzGame(object):
def fizzBuzz(self, n):
fb_list = []
#Iterate over the range
for i in range(1,n+1):
if i% 3== 0 and i%5==0:
fb_list.append("FizzBuzz")
elif i %3==0:
fb_list.append("Fizz")
elif i% 5 == 0:
fb_list.append("Buzz")
else:
fb_list.append(str(i))
return fb_list
ob1 = FizzBuzzGame()
print(ob1.fizzBuzz(100))
Output of the above code-
['1', '2', 'Fizz', '4', 'Buzz', 'Fizz', '7', '8', 'Fizz', 'Buzz',
'11', 'Fizz', '13', '14', 'FizzBuzz', '16', '17', 'Fizz', '19', 'Buzz', 'Fizz',
'22', '23', 'Fizz', 'Buzz', '26', 'Fizz', '28', '29', 'FizzBuzz', '31', '32',
'Fizz', '34', 'Buzz', 'Fizz', '37', '38', 'Fizz', 'Buzz', '41', 'Fizz', '43',
'44', 'FizzBuzz', '46', '47', 'Fizz', '49', 'Buzz', 'Fizz', '52', '53', 'Fizz',
'Buzz', '56', 'Fizz', '58', '59', 'FizzBuzz', '61', '62', 'Fizz', '64', 'Buzz',
'Fizz', '67', '68', 'Fizz', 'Buzz', '71', 'Fizz', '73', '74', 'FizzBuzz', '76',
'77', 'Fizz', '79', 'Buzz', 'Fizz', '82', '83', 'Fizz', 'Buzz', '86', 'Fizz',
'88', '89', 'FizzBuzz', '91', '92', 'Fizz', '94', 'Buzz', 'Fizz', '97', '98',
'Fizz', 'Buzz']
- 1 and 2 are not multiples of either 3 or 5, so it prints the same number.
- 3 is a multiple of 3, so it prints Fizz.
- 5 is a multiple of 5, so it prints Buzz.
- 6 is a multiple of 3, so it prints Fizz.
- 7 and 8 are not multiples of either 3 or 5, so it prints the same number.
- 9 is a multiple of 3, so it prints Fizz.
- 10 is a multiple of 5, so it prints Fizz.
- 11 is not a multiple of either 3 or 5, so it prints the same number.
- 12 is a multiple of 3, so it prints Fizz.
- 13 and 14 are not multiples of either 3 or 5, so it prints the same number.
- 15 is a multiple of both 3 and 5, so it prints FizzBuzz.
Related Articles
Python program to multiply two numbers
Python program to count the occurrences of a word in a text file
Python program to input week number and print week day
Python program to map two lists into a dictionary
Python OpenCV Histogram of Grayscale Image
Find the stop words in nltk Python
Convert list to dictionary Python
Convert array to list Python
numpy dot product
glob in Python
Python heap implementation
zip function in Python
Remove last element from list Python
Check if list is empty Python
Print alphabet pattern in python
Hollow Diamond Pattern in Python
Reverse pyramid in python
Python random choice
Python dict inside list
Remove character from string Python
Python raise keyword