def freshVarname(startingName, namesUsed):
    """pick a variant on 'startingName' that's not in namesUsed"""
    if startingName not in namesUsed:
        return startingName
    ix = 1
    while True:
        name = startingName + "_" + str(ix)
        if name not in namesUsed:
            return name
        ix = ix + 1