# Go deh!

Mainly Tech projects on Python and Electronic Design Automation.

## Sunday, March 04, 2007

### Fizz-Buzz.py

I am having to set some extra work at home for my ten year old son. I found information on a game Fizz-Buzz and introduced it to my son, only to find that he had already played a variant at school.

Oh well.

By that time I had already created a python program to check his answers (written in simplistic style as I intend one day to get all of my kids to learn Python).

`'''Fizz-Buzz.pyFizz-Buzz Game.Instructions for two players, 3-5 game. (Easiest).   1. Players take it in turns counting from one to ninety-nine.      (Don't take too much or too little time between numbers       - try for a steady pace)   2. Player must say Fizz-Buzz instead of their number if the number is a      multiple of three and of five.   3. Otherwise, player must say Fizz instead of their number if the number      is a multiple of three.   4. Otherwise, player must say Buzz instead of their number if the number      is a multiple of Five. Digit version  Use basic instructions 1-4 as above, then add the following rules:   5. Otherwise, player must say Fizz for each digit in the number that is      a three,      And player must also say Buzz for each digit in the number that is a      five.      (for example, say Fizz-one instead of 31, Fizz-two instead of thirty      two)   6. Remember, the rules must be applied in order so  thirty three is      actually Fizz and not Fizz-Fizz as it is first a multiple of three'''fizz = 3buzz = 5digits_version = Truefor tens in (0,1,2,3,4,5,6,7,8,9):   for units in (0,1,2,3,4,5,6,7,8,9):       number = tens*10 + units       if number == 0:           pass       else:           print "For", number, "Say",           if (number % fizz) == 0 and (number % buzz) == 0:               print "FizzBuzz"           elif (number % fizz) == 0:               print "Fizz"           elif (number % buzz) == 0:               print "Buzz"           elif digits_version:               saytens = tens               sayunits = units               digits_as_fizzbuzz = False               if tens == fizz:                   saytens = "Fizz"                   digits_as_fizzbuzz = True               if units == fizz:                   sayunits = "Fizz"                   digits_as_fizzbuzz = True               if tens == buzz:                   saytens = "Buzz"                   digits_as_fizzbuzz = True               if units == buzz:                   sayunits = "Buzz"                   digits_as_fizzbuzz = True               if digits_as_fizzbuzz:                   print saytens,sayunits               else:                   print number           else:               print number`

#### 7 comments:

1. I think you just need to checks.. the FizzBuzz case would be covered automatically if you just put two if statements in sequence :)

2. Thanks Riffraff,
I had seen that technique later, on a thread from Reddit I think. It just didn't occur to me at the time, so its their in 'long-hand'

- Paddy.

3. You don't need to have that whole "if" chain for the digit version. That can be done with a simple string replacement. Here's my version:

def fizzbuzz(num, digit_version=True, fizz=3, buzz=5):
say = ''
if (num % fizz) == 0:
say += 'Fizz'
if (num % buzz) == 0:
say += 'Buzz'
say = say or str(num)
if digit_version:
say = say.replace(str(fizz), 'Fizz').replace(str(buzz), 'Buzz')
return say

for num in xrange(1, 100):
print fizzbuzz(num)

4. for i in range(1,101):
if not i%15:
print "FizzBuzz"
continue
if not i%5:
print "Buzz"
continue
if not i%3:
print "Fizz"
continue
print i

1. your syntax can't be correct for python interpreter, as this other python code is correctly interpreted and run in interactive mode...

for i in ("fizzbuzz" if i % 15 == 0 else ("fizz" if i % 3 == 0 else ("buzz" if i % 5 == 0 else i)) for i in range(1, 101)): print i

I always find myself puzzled over having no clue about system interpreter/compiler characteristics when learning a programming language merely on a developer's point of view for developing and delivering programs for end users.. btw, if above lines of code can only be read as pseudo-code. That is properly the real reason behind this fizzbuzz test for 'natural' selection of 'good' coder, not thinker...

2. Badvocato: the code worked.

3. Badvocato: the code worked.