My function closest_power(base, num) failing some test cases
<pre><code>def closest_power(base, num):
    '''
    base: base of the exponential, integer &gt; 1
    num: number you want to be closest to, integer &gt; 0
    Find the integer exponent such that base**exponent is closest to num.
    Note that the base**exponent may be either greater or smaller than num.
    In case of a tie, return the smaller value.
    Returns the exponent.

    '''

    result=0
    exp=1
    while base**exp&lt;num:
        if base**exp &lt;= num &lt; base**(exp+1):
           result = exp

        elif num - base**exp &lt;= base**(exp+1) - num:
           result=exp+1

        exp+=1
    return result
</code></pre>

<p>In my code, when I try running <code>closest_power(4,62)</code> it returns <code>2</code> instead of <code>3</code> and, in similar test cases like <code>closest_power(4, 12)</code> returns <code>1</code> instead of <code>2</code>. (<code>closest_power(5, 22)</code> returns <code>1</code> instead of <code>2</code>) </p>

<p>For rest of the test cases it works fine for example: </p>

<pre><code>closest_power(2, 384.0) 
</code></pre>

<p>returns <code>8</code>.</p>

<p>Why am I missing out those cases?</p>

<p>this works.</p>

<pre><code>def closest_power(base, num):
    bevaluate = True
    exponent = 0
    vale = 0 
    older = 0
    olders = 0
    while bevaluate:
        vale = base ** exponent
        if vale &lt; num:
            older = num - vale
            exponent+=1
        else:
            olders = vale - num
            if older == olders :
                if num == 1:
                    exponent = 0
                else:
                    exponent-=1
                break
            elif older &lt; olders:
                exponent-=1
                break            
            bevaluate = False

    return  exponent
</code></pre>

python python-3.x