Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Hassan K
Hassan K
1,556 Points

I initiated "is_fizz" and "is_buzz" using "None" and used nested if statements, is that bad or unclear code?

My code:

name = input("Please enter your name: ")
number = input("Please enter a number: ")
number = int(number)
print ("Hi {}, I see your number is {}".format(name, number))

if int(number) % 3 == 0:
    print ("is a Fizz number.")
    if int(number) % 5 == 0:
        print ("is a Buzz number.")
        if int(number) % 3 == 0 and int(number) % 5 == 0:
            print ("is a FizzBuzz number")
else:
    print("is neither a fizzy or a buzzy number.")

is_fizz = None
is_buzz = None
is_fizzbuzz = None
if int(number) % 3 == 0:
    is_fizz = True
    if int(number) % 5 == 0:
        is_buzz = True
        if int(number) % 3 == 0 and int(number) % 5 == 0:
            is_fizzbuzz = True
else:
    is_fizz = False
    is_buzz = False
    is_fizzbuzz = False

if is_fizz == True:
    print ("Fizz is True")
    if is_buzz == True:
        print ("Buzz is True")
        if is_fizzbuzz == True:
            print ("Fizzbuzz is True")
else:
    print ("all are false")

is this too much code to solve this problem? I'm trying to think of where I can combine code to make my code more succinct but not getting anywhere

1 Answer

Steven Parker
Steven Parker
231,007 Points

The nesting is making it more complex than necessary, and it's also impairing the functionality. The "buzz" test wont be done unless the number has already passed the "fizz" test. Try re-writing it without the nesting.

Also, I don't think the separate logic testing is part of the exercise. But when working with logic, it's more conventional for an item that is not "True" to be "False" (instead of "None").