Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Comparison Challenge

Brianda Lopez
Brianda Lopez
8,318 Points

I cannot seem to get a "FizzBuzz" result. What am I doing wrong?

I have successfully returned Fizz and Buzz, but even with integers 15 and 30 I am not getting. FizzBuzz! Any assistance is appreciated.

name = input("Please enter your name: ")
number = int(input("Please enter a number: "))
print("Your number is",number,"...")
if number % 3 == 0:
    print(number,"is a Fizz number!")
elif number % 5 == 0:
    print(number,"is a Buzz number!")
elif number % 3 == 0 and number % 5 == 0:
    print(number," is a FizzBuzz number!")
    print(number,"is neither a fizzy or a buzzy number.")
Samuel Almanza
Samuel Almanza
4,426 Points

Hi Brianda,

Your statements are written correctly, but the order of the statements is important.

Remember that conditions are evaluated in order and if the condition is true, then it runs the command and your code block ends.

Let's look at your code step by step: First, the computer will check to see if number % 3 == 0. If this is true, the code carries out the print statement and ends.

If the condition is false, then the computer moves to the next line of code and checks if number % 5 == 0. If this is true, the code carries out the print statement and ends.

If these are both false, the computer evaluates the conditions of the next line and so on.

Hope this helps!

2 Answers

Jeff Muday
Jeff Muday
Treehouse Moderator 26,896 Points

Your code parses properly, but!

.. be careful ... Python does the comparisons sequentially so you would never see a "FizzBuzz" because it would test as a "Fizz" number first.

Make the "FizzBuzz" test first and it will work.

Brianda Lopez
Brianda Lopez
8,318 Points

Thank you both so much! I can't believe I made such a silly error!