Implementation of FizzBuzz game in python
In this tutorial, we will learn how to implement the FizzBuzz game in Python. By the help of python, we can implement different types of games.
Here are some examples:
First of all, we will know what is the logic behind this game. In this game, there is multiplayer envolve they sit on a round table and start counting from 1 to 100. If the number came which is divisible by 3 then instead of saying that number the player will say fizz. And if the number is divisible by 5 then the corresponding player will say buzz. And if the number is divisible by both number(eg. 3 and 5) then the corresponding player has to say fizzbuzz
How to create FizzBuzz game in Python
To implement this game we should have knowledge about the control flow statement and the looping concept of the python.
so let us see how its work
for i in range(1,31): if i%3==0 and i%5==0: print("fizzbuzz") elif i%3==0: print("fizz") elif i%5==0: print("buzz") else: print(i)
As we define in the logic we have taken the numbers from 1 to 30 and check that the number is divisible by 3 and 4 or not if yes, then it will print fizzbuzz and if no then it will again check that if it is divisible by 3 if yes, then print fizz, if yes then again check for the number is divisible by 5, if yes than print buzz. if no then it will simply print the corresponding number in the else part.here we are using ladder if else.
Output:-
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
As we can see in the above output at the place of 15 and 30 there is fizzbuzz and the number which is divisible by 3 has replaced by fizz and the number which is divisible by 5 has been replaced by buzz.
You can also search for
Leave a Reply