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 Comparison Solution

Amy Mou
Amy Mou
8,920 Points

Another Approach, Is it good?

This is my approach, it works. Wondering if it's fine? name = input("Please enter your name: ") number = int(input("Please enter a number: "))

TODO: Make sure the number is an integer

TODO: Print out the User's name and the number entered,

making sure the two statements are on separate lines of output.

print("Hello {}! ".format(name)) print("The number {}...".format(number))

TODO: Compare the number the user gave with the different

FizzBuzz conditions.

*********************

If the number is divisible by 3, print "is a Fizz number."

If the number is divisible by 5, print "is a Buzz number."

If the number is divisible by both 3 and 5, print "is a FizzBuzz number."

Otherwise, print "is neither a fizzy or a buzzy number."

*********************

if number/15 == int(number/15): print("is a FizzBuzz number.") elif number/3 == int(number/3): print("is a Fizz number.") elif number/5 == int(number/5): print("is a Buzz number") else: print("is neither a fizzy or a buzz number.")

TODO: Define variables for is_fizz and is_buzz that stores

a Boolean value of the condition. Remember that the modulo operator, %,

can be used to check if there is a remainder.

Using the variables, check the condition of the value, and print the necessary

string

4 Answers

Liam Trant
Liam Trant
2,918 Points

Hi Amy!

Objectively, yes it is good since the game functions as expected. However, the TODOs in the exercise are suggested so that we can practice using techniques we've learned which will eventually help enable us to write more efficient code.

I too first had a working solution, but I realized that my code wasn't actually assigning boolean values to the is_fizz and is_buzz variables. Also, while my "if, elif" logic was returning boolean values correctly, the body of my logic was using all ints. I also misunderstood how the modulo % was supposed to be used. All of this didn't dawn on me until I watched the solution video!

Son-Hai Nguyen
Son-Hai Nguyen
2,481 Points

Hi Amy,

It's an interesting concept you got there! I myself came up with a solution of dividing the number for 15 instead of using the "and" function boolean. I think it works fine so far.

Cheers.

My code failed!!!!

I try this challenge in another way. I used method .is_integer() to valid if is true or false.

if (number/3).is_integer():
    if (number/5).is_integer():
        print("{} is a FizzBuzz number.".format(number))
    else:
        print("{} is a Fizz number.".format(number))
elif (number/5).is_integer():
    print("{} is a Buzz number.".format(number))
else:
    print("{} is neither a fizzy or a buzzy number.".format(number))