# I passed a fizzbuzz test but failed at recursion.

Bill bsagert at gmail.com
Wed Mar 10 16:55:27 CET 2010

```Look at this recursive fizzbuzz function from
http://www.codinghorror.com/blog/2007/02/why-cant-programmers-program.html

def fizzbuzz(num):
if num:
if num % 15 is 0: return fizzbuzz(num-1) + 'fizzbuzz \n'
elif num % 5 is 0: return fizzbuzz(num-1) + 'buzz \n'
elif num % 3 is 0: return fizzbuzz(num-1) + 'fizz \n'
else : return fizzbuzz(num-1) + ('%d \n' % num)
return ''
print fizzbuzz(100)

This returns "1 2 fizz 4 ...etc... 97 98 fizz buzz" which is correct.

However, when I try to decipher the logic of the function I imagine
the solution reversed as "buzz fizz 98 97 ...etc... 4 fizz 2 1".

After all, the first num is 100 which decrements by one until a zero
stops the recursive loop.

My (faulty) reasoning is that fizzbuzz(100) would firstly print a
"fizz" and  the last fizzbuzz(1) would finally print a "1".

My logic is wrong, but I don't know why.

```

More information about the Python-list mailing list