patternpythonMajor
Ultra-Beginner Python FizzBuzz ... Am I missing something?
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 + 1Solution
Lose the useless brackets
This:
can just be:
Increment out of the
Wouldn't be easier to do:
A
I've also renamed
Let's use only one
Why do 4 different print, when what is really changing is the printed message?
Light bulb!
Let's try this one:
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:
IMHO it would be better to do:
There! Now with:
is clear that
This:
while (count < 101):can just be:
while count < 101:Increment out of the
ifsWouldn'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 loopA
for loop will be betterfor 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 numI'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
printWhy 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 msgLight 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 msgCopy 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 numThere! Now with:
print msg or numis 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 loopfor 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 numfor 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 msgContext
StackExchange Code Review Q#9751, answer score: 45
Revisions (0)
No revisions yet.