Python Course FizzBuzz Solution Incorrect? Am I wrong?

I did the FizzBuzz challenge exercise found here: https://codewithmosh.com/courses/417695/lectures/6781645

As Mosh explains, even developers with years of experience get this incorrect. I decided to attempt it and I was satisfied with my solution.

def fizz_buzz(input):
fizzbuzz = input % 15 == 0
fizz = input % 3 == 0
buzz = input % 5 == 0
if input == 0:
return input
if fizzbuzz == True:
return “fizzbuzz”
if fizz == True:
return “fizz”
if buzz == True:
return “buzz”
return input

print(fizz_buzz(15))

Mosh states his code is the cleanest possible solution to this problem, and I get why it is cleaner, but he fails to account for 0 as the input and therefore failed his own exercise… am I wrong?

This is his code…


def fizz_buzz(input):
if (input % 3 == 0) and (input % 5 == 0):
return “FizzBuzz”
if input % 3 == 0:
return “Fizz”
if input % 5 == 0:
return “Buzz”
return input

print(fizz_buzz(0))

My indentation was destroyed…

My code is below

def fizz_buzz(input1):

if input1 == 0 or (input1 % 3 != 0 and input1 % 5 != 0):
    return input1

if input1 % 3 == 0:
    if input1 % 5 == 0:
        return "FizzBuzz"
    return "Fizz"
else:
    return "Buzz"

I would not take the lesson too literally. If you understood the concept he was trying to explain then you are in ok shape.

Your fizzbuzz, fizz, and buzz variables are all returning Booleans. Your if statement’s conditions are checking for a Boolean, no? You don’t need to compare true to true or false to false.


if fizzbuzz: # <= fizzbuzz variable is already true or false
    # run code

I had tried the exercise to.
And my code works fine.

I can´t realy understood why he didn’t use elif statements.

I copy my code:

def fizz_buzz(dividendo):
if (dividendo % 3 == 0) and (dividendo % 5 == 0):
return (“FizzBuzz”)
elif dividendo % 3 == 0:
return (“Fizz”)
elif dividendo % 5 == 0:
return (“Buzz”)
else:
return (dividendo)

print(fizz_buzz(45))

I’m gona copy the answer that he give us in the course:

Could someone explain me why?

(Sorry if my gramar isn’t good, english is not my native langage)

Thank U

It’s just a matter of style.

if the true branch of an if ends with a return statement a statement on the same level of indention as if the if can only be reached in the false case. So you don’t need to explicitly use else or elif if you want to execute code only in the false case.

This is my bad code before I’ll see Mr.Mosh’s answer.

def fizz_buzz(a, b=1) :
if a % 3 == 0:
a = “fizz”
return a
elif a * b == 15:
a = “fizz_my_buzz”
return a
elif a % 5 == 0:
a = “buzz”
return a
else:
return"fizz_buzz"

print(fizz_buzz(5,3))

The solution to your question is actually very easy, it looks like Mr Hamedani forgot to mention the 0 and negative integer numbers(like -3 and -5) input, here’s my answer added to Mr Mosh’s solution(line 2 and 3):

def fizz_buzz(input):
if input <= 0:
return input
if (input % 3 == 0) and (input % 5 == 0):
return “fizz_buzz”
if input % 3 == 0:
return “fizz”
if input % 5 == 0:
return “buzz”
return input

print(fizz_buzz(input))

Have a good day…