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

Cannot figure out why my elif are not working..

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.

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."

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

print(name,"\n", number) if (number / 3): print("{} is a Fizz number.".format(number)) elif (number / 5): print("{} is a Buzz number.".format(number)) elif (number / 3 & number / 5): print("{} is a FizzBuzz number.".format(number)) elif (number/3 or number/5): print("{} is neither a fizzy or a buzzy number.".format(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

I tried using % and tried removing the parenthesis but no luck! I need help :(

1 Answer

You'll want to use the modulo operator. In your code if (number / 3) is always true (except if number = 0 ) so the rest of the conditions are never considered.

One solution:

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

print(name,"\n", number)
if (number % 3 == 0):
    if (number % 5 == 0):
        print("{} is a FizzBuzz number.".format(number))
    else:
        print("{} is a Fizz number.".format(number))
elif (number % 5 == 0):
    print("{} is a Buzz number.".format(number))
else:
    print("{} is neither a fizzy or a buzzy number.".format(number))