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

Adam Kroner
Adam Kroner
1,460 Points

Comparison Solution Help

I think this almost works but I do not understand why it always prints my else statement?

name = input("Please enter your name: ") number = input("Please enter a number: ")

number = int(number)

print("Hello{},\nthe number {}...".format(name, number))

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

2 Answers

Steven Parker
Steven Parker
231,268 Points

When posting code, you need to use Markdown formatting to preserve the appearance (particulary indentation); and without seeing the indentation it's impossible to predict how the code might behave.

But two issues visible as it is:

  • tests in a chain (after the first one) should use "elif" instead of "if"
  • a compound condition ("FizzBuzz") must be tested before the individual conditions
Adam Kroner
Adam Kroner
1,460 Points

Heard sir I will try those, apologies on the construction in the question. It appeared alright when I copy and pasted but I did not check what it looked like on the forum.