You just need to replace all the places in the algorithm where "base-10" is used with base 8. The only place this really is is when we turn the number into a string, so we can square each "digit". To determine digits, we need a base. Normally, we choose base 10, as your sample shows. Converting an integer to a string in an arbitrary base (or, in your case 8) has been answered here.
We might also adjust the lookup table SQUARE
.
def to_base_8(n):
digits = []
while n > 0:
digits.append(str(n % 8))
n = n // 8
return ''.join(reversed(digits))
SQUARE = dict([(c, int(c)**2) for c in "01234567"])
def is_happy(n):
s = set()
while (n > 1) and (n not in s):
s.add(n)
n = sum(SQUARE[d] for d in to_base_8(n))
return n == 1
a=is_happy(28)