Nonlocal: Difficult Solutions

code writing

Write a function that takes in a number, num, and a single digit, digit and removes all instances of digit from num.
def remove_digit(num, digit):
>>> remove_digit(1232142, 2)
1314
    reversed, count, final = 0, 0, 0
    def helper(n):
        nonlocal reversed, count
        if n != digit:
            reversed = reversed * 10 + n
            count += 1
    while num > 10:
        last, num = num % 10, num // 10
        helper(last)
    helper(num)
    while count > 0:
        final = final * 10 + reversed % 10
        reversed = reversed % 10
        count -= 1
    return final