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

Marlon Jay Chua
Marlon Jay Chua
4,281 Points

fizzbuzz condition

if (number % 3 or number % 5) == 0: I used this statement for fizzbuzz condition, can this also be correct? although it worked.

2 Answers

Steven Parker
Steven Parker
229,708 Points

It works, but it's "obfuscated" (difficult to read). It relies on the fact that Python considers False to be equal to 0.

A more clear way to test for the same condition would be:

if number % 3 == 0 and number % 5 == 0:
Gabe Medina
Gabe Medina
4,688 Points

FizzBuzz can be solved a number of different ways, as you probably know.

The logic for FizzBuzz should be using and as the condition for the test. Something like this:

for number in range(1, 101):
    if number % 3 == 0 and number % 5 == 0:
        print ('FizzBuzz')

Then, to print out the 'Fizz' and 'Buzz' conditions, just use some elif statements to finish the loop.