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

How do I define a variable that's a boolean value of a condition?

I'm working on the fizzbuzz python challenge linked below:

https://teamtreehouse.com/library/comparison-challenge

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.

The challenge says the above but I don't understand what that means. In case the challenge is not clear, the user should input a number, and then there will be output saying whether the input is divisible by 3, 5, both, or neither.

Thanks in advance for any help!

2 Answers

It may help to break it down by keywords.

Define variables for is_fizz and is_buzz. This means that we need to make new variables with these names.

is_fizz = 
is_buzz = 

Stores a boolean value of the condition. Remember, a boolean value is a True or False statement. You can use a double equals sign to figure out if two values match for instance, 1 == 1 would return True, and 1 == 2 is False! So we know we need the double equals somewhere in our code. We don't know what we are comparing yet, so lets just leave the values on the left and right side of the double equals blank

is_fizz =  ==
is_buzz =  ==

The modulo operator % can be used to check if there is a remainder. The % operator will divide two numbers and return the remainder. If a remainder is zero, it means that the first number is divisible by the second. So, we would put the operation on the left side of the double equals, and zero on the right, because we are comparing the operation to what we want as the answer.

is_fizz = number % 3 == 0
is_buzz = number % 5 == 0

If you open the REPL and set number = 3, you could type in our variables we just created and it should return:

is_fizz
True

is_buzz
False

Hope this helps!

Thank you so much Jamison Habermann ! That approach definitely helps clear things up for me. I was able to solve the challenge.

To piggy back off of Jamison Habermann, you can wrap this into a function like so:

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

def fizz_buzz(number=number):

    is_fizz = number % 3 == 0   # e.g. 9, 3, 12 are all True
    is_buzz = number % 5 == 0   # e.g. 5, 15 are all True

    if is_fizz and is_buzz:
        # both are True then...
        # e.g. 15, 30
        print('is a FizzBuzz number.')

    elif is_fizz:
        print("is a Fizz number.")

    elif is_buzz:
        is_buzz = True
        print("is a Buzz number")

    else:
        # e.g. 8 % 3 is 2 and 8 % 5 is 3
        print("is neither a fizzy or a buzzy number")

Hi Cameron! That's interesting. I tried playing with the "def" keyword before, but I didn't really know what I was doing with it. I tried just copy/pasting the code you had here into a new file, but I think I'm missing something. How do I get the input to run through that function? When I ran the above, it seems to stop after the the number input.