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

Is there is way to make this cleaner?

is there a way to make this any cleaner? or to make "if is_fizz and is_buzz" be the last option on the list? or does it have to be the first one?

Edit, how do i add a screenshot of my work?

https://w.trhou.se/6m4704limq

Chris Freeman
Chris Freeman
Treehouse Moderator 67,636 Points

To link to an image, save the image at an external URL, say, on Imgur. Then link to image use the format ![alt text](/path/to/img.jpg "Title")

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 67,636 Points

You code is as straightforward as can be expected. The if/elif/else is the only choice for now.

Coming in Python 3.10, there will be a match/case statement as defined in PEP 634.

match (number % 3 == 0, number % 5 == 0):
    case (True, False):
        print(f"{number} is Fizz")
    case (False, True):
        print(f"{number} is Buzz")
    case (True, True):
        print(f"{number} is FizzBuzz")
    case _:
        print(f"{number} is neither Fizz nor Buzz")