HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMajor

Ultra-Beginner Python FizzBuzz ... Am I missing something?

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
fizzbuzzbeginnerpythonultramissingsomething

Problem

I just started programming in Python this morning, and it is (more or less) my first programming language. I've done a bit of programming before, but never really did much except for "Hello World" in a few languages. I searched around for some Python FizzBuzz solutions, and they all seem significantly more complicated then mine, so I think I must be missing something, even though it works correctly. Could you guys point out any errors I've made, or things I can improve?

count = 0
while (count < 101):
    if (count % 5) == 0 and (count % 3) == 0:
        print "FizzBuzz"
        count = count +1
    elif (count % 3) == 0:
        print "Fizz"
        count = count + 1
    elif (count % 5) == 0:
        print "Buzz"
        count = count +1
    else:
        print count
        count = count + 1

Solution

Lose the useless brackets

This:

while (count < 101):


can just be:

while count < 101:


Increment out of the ifs

Wouldn't be easier to do:

count = 0
while count < 101:
    if count % 5 == 0 and count % 3 == 0:
        print "FizzBuzz"
    elif count % 3 == 0:
        print "Fizz"
    elif count % 5 == 0:
        print "Buzz"
    else:
        print count

    count = count + 1    # this will get executed every loop


A for loop will be better

for num in xrange(1,101):
    if num % 5 == 0 and num % 3 == 0:
        print "FizzBuzz"
    elif num % 3 == 0:
        print "Fizz"
    elif num % 5 == 0:
        print "Buzz"
    else:
        print num


I've also renamed count to num since it doesn't count much, is just a number between 1 and 100.

Let's use only one print

Why do 4 different print, when what is really changing is the printed message?

for num in xrange(1,101):
    if num % 5 == 0 and num % 3 == 0:
        msg = "FizzBuzz"
    elif num % 3 == 0:
        msg = "Fizz"
    elif num % 5 == 0:
        msg = "Buzz"
    else:
        msg = str(num)
    print msg


Light bulb!

"FizzBuzz" is the same of "Fizz" + "Buzz".

Let's try this one:

for num in xrange(1,101):
    msg = ''
    if num % 3 == 0:
        msg += 'Fizz'
    if num % 5 == 0:       # no more elif
        msg += 'Buzz'
    if not msg:      # check if msg is an empty string
        msg += str(num)
    print msg


Copy and paste this last piece of code here to see what it does.

Python is a very flexible and powerful language, so I'm sure there could be other hundred and one different possible solutions to this problem :)

Edit: Improve more

There's still something "quite not right" with these lines:

if not msg:
    msg += str(num)


IMHO it would be better to do:

for num in xrange(1,101):
    msg = ''
    if num % 3 == 0:
        msg += 'Fizz'
    if num % 5 == 0:
        msg += 'Buzz'
    print msg or num


There! Now with:

print msg or num


is clear that num is the default value to be printed.

Code Snippets

while (count < 101):
while count < 101:
count = 0
while count < 101:
    if count % 5 == 0 and count % 3 == 0:
        print "FizzBuzz"
    elif count % 3 == 0:
        print "Fizz"
    elif count % 5 == 0:
        print "Buzz"
    else:
        print count

    count = count + 1    # this will get executed every loop
for num in xrange(1,101):
    if num % 5 == 0 and num % 3 == 0:
        print "FizzBuzz"
    elif num % 3 == 0:
        print "Fizz"
    elif num % 5 == 0:
        print "Buzz"
    else:
        print num
for num in xrange(1,101):
    if num % 5 == 0 and num % 3 == 0:
        msg = "FizzBuzz"
    elif num % 3 == 0:
        msg = "Fizz"
    elif num % 5 == 0:
        msg = "Buzz"
    else:
        msg = str(num)
    print msg

Context

StackExchange Code Review Q#9751, answer score: 45

Revisions (0)

No revisions yet.