11/08/2020

Python explanation (Edabit // Fizz Buzz Interview Question)

Hello!
Today is going to be Python Explanation!
๐Ÿ: ME?๐Ÿ˜
Leo618 : Uh, no...๐Ÿ˜“
๐Ÿ : Oh.....๐Ÿ˜’
Leo618 : Sorry, uh..... Never mind.๐Ÿ˜…
 The question is this!

Create a function that takes a number as an argument and returns "Fizz""Buzz" or "FizzBuzz".

  • If the number is a multiple of 3 the output should be "Fizz".
  • If the number given is a multiple of 5, the output should be "Buzz".
  • If the number given is a multiple of both 3 and 5, the output should be "FizzBuzz".
  • If the number is not a multiple of either 3 or 5, the number should be output on its own as shown in the examples below.
  • The output should always be a string even if it is not a multiple of 3 or 5.

Examples

fizz_buzz(3) ➞ "Fizz"

fizz_buzz(5) ➞ "Buzz"

fizz_buzz(15) ➞ "FizzBuzz"

fizz_buzz(4) ➞ "4"
So, this is the solved code!!
def fizz_buzz(num):
if num % 3 == 0 and num % 5 == 0:
return "FizzBuzz"
if num % 3 == 0:
return "Fizz"
if num % 5 == 0:
return "Buzz"
else:
return str(num)

I'll change the font...๐Ÿ˜Ž(WHY!)
so, Let's see the code.
The first if is when the number is a multiple of either 3 or 5.
If the answer is 0 when the number ModulO with 3, AND when the answer is 0 when the number Modulo with 5.
Then, we return "FizzBuzz"!
The second if  is when the number is a muLtiple of 3.
If the Answer is 0 when the number Modulo with 3, then we return "Fizz"!
The third if is when the number is a multiple of 5.
If the answer is 0 when the number modulo with 5, then we return "BUZZ"!
Lastly, when the number is not a multiple of 3 or 5, Return the number Stringed.
How was it?
It was really easy!
Piece of a cake!
Bye!!!


0 ไปถใฎใ‚ณใƒกใƒณใƒˆ:

Super Leo 4!

  Leo : Let's do this! Reimu : I'll kill those monsters, so you go! Leo : Yeah! I'll go to the goal!  Leo : Let's go〜! ? Wai...