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

Is there a cleaner and better way to write the 'comparison challenge' code?

Hi,

This is with regards to the "Comparison Challenge"

After reviewing Ken's video, I noticed that my code is lengthy and repetitive. I'm thinking how can I improve (shorten and concise) my code and its indentation which I had written for this challenge, apart from what was shown in the video solution. l'll love to seek advice and opinions from the community~

Thank you in advance!

Here's the full set of code which I had written for the challenge:

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

print ("Hi {}, welcome~".format(name))
print ("{}, you have entered the number {}".format(name, number))

if number % 3 == 0 and number % 5 == 0:
    print (" {} is a FizzBuzz number.".format(number))

elif number % 3 == 0:
    print (" {} is a Fizz number.".format(number))

elif number % 5 == 0:
    print (" {} is a Buzz number.".format(number))

else:
    number % 3 != 0 and number % 5 != 0
    print ("{} is neither a Fizzy nor Buzzy number.".format(number))

3 Answers

How about

def fizzbuzz(number):
    fizz_or_buzz = {3: 'Fizz', 5: 'Buzz'}
    response = ''
    for n in fizz_or_buzz.keys():
        if number % n == 0:
            response += fizz_or_buzz[n]
    if response:
        return str(number) + ' is a {} number'.format(response)
    else:
        return str(number) + ' is neither a Fizzy nor Buzzy number'   

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

Hi Stuart thank you very much for your assistance, learnt another way to write it through your guidance.

I'm just curious how does this line of code in your version works:

fizz_or_buzz = {3: 'Fizz', 5: 'Buzz'}

Hehehe, thanks once again Sensei~ Arigato~~

Hi there, no problem.

def fizzbuzz(number):
    fizz_or_buzz = {3: 'Fizz', 5: 'Buzz'} # this dictionary is used to get the 3 and 5 for the test and the Fizz  and Buzz for the response
    response = ''
    for n in fizz_or_buzz.keys(): # iterate through the dictionary keys i.e. 3 and 5 
        if number % n == 0:
            response += fizz_or_buzz[n] # add to response either Fizz or Buzz or both 
    if response: # do we have a response 
        return str(number) + ' is a {} number'.format(response)
    else: # no
        return str(number) + ' is neither a Fizzy nor Buzzy number'   

Wow!! Thank you very much Stuart!! Now its Crystal clear~~~ LOL!!