Trinary Number in Binary Deployment level

I managed to finish the binary part, but I can’t seem to make my code to work on for the advanced troop I though I had everything right, but I get extra characters wich mess up the first and the last digit of the trinary number.

def summonTrinary(deploy2, trinarya, trinaryb):
    if deploy2 >= trinarya and deploy2 < trinaryb:
        hero.summon("griffin-rider")
        deploy2 = deploy2 - trinarya
        return deploy2
    elif deploy2 >= trinaryb / 3 and deploy2 < trinarya:
        deploy2 = deploy2 - trinaryb / 3
        hero.summon("archer")
        return deploy2
    else:
        hero.summon("soldier")
        return deploy2
Poslist = [20, 25, 30, 35, 40, 45, 50, 55]
warlocks = hero.findByType("warlock")
i2 = 0
while i2 < len(warlocks):
    deploy2 = warlocks[i2].deployment
    a = 13122
    b = 19683
    test = 1
    while b > 1:
        a = a / 3
        b = b / 3
        deploy2 = summonTrinary(deploy2, a, b)
    bindex = len(hero.built) - 8
    while bindex < len(hero.built):
        friend = hero.built[bindex]
        unitx = bindex % 8
        hero.command(friend, "move", {"x": Poslist[unitx], "y": warlocks[i2].pos.y})
        bindex += 1
    i2 += 1