Python Fizzbuzz Code-Fizzbuzz Algorithm in Python-DecodingDevOps
Python Fizzbuzz Code-Fizzbuzz Algorithm in Python-DecodingDevOps
What is Fizzbuzz in Python
FizzBuzz is a simple programming task that is used to identify the FizzBuzz numbers among a group of certain numbers. if a number is divisible by 3 print the word "Fizz" instead of the number, if the number is divisible by 5 print the word "Buzz". When the number is divisible by both 3 and 5 print the whole word "FizzBuzz" otherwise just print the particular number. We can test the divisibility using the python modulo operator which divides both the numbers and returns the remainder. In python, modulo is denoted by the symbol(%). In this post i am going to explain python fizzbuzz code algorithm.
Python Fizzbuzz Code
for i in range(1,101): variable = '' if i % 3 == 0: variable += "Fizz" if i % 5 == 0: variable += "Buzz" if not line: variable += str(i) print(variable)
STEP 1: In this step, we will loop through all the numbers between 1 to 100.
STEP 2: In this step, we set a variable line to an empty string.
STEP 3: In this step, we will check whether the number is divisible by 3 if the number is divisible by 3 add the word "Fizz" to the variable.
STEP 4:In this step, we will check whether the number is divisible by 5 , if the number is divisible by 5 add the word "Buzz" to the variable.
STEP 5: In this step, we will check whether the number is divisible by both 3 and 5 if its divisible by both the numbers print the word "FizzBuzz".Then we add i to the empty string if the string is still empty. Notice that an empty string returns false.