Python Course FizzBuzz Solution Incorrect? Am I wrong?

I did the FizzBuzz challenge exercise found here: 12- Exercise | Code with Mosh

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