'recursion function do not return value [duplicate]

I am trying to write a function that takes an integer and a string(1234,"3") the function should find if the integer contain the value of the string via recursion. so I wrote the following code:

def has_digit_recursion(num,string):
    num_s = str(num)
    if num_s[-1] == string:
        return True
        
    num //=10
    has_digit_recursion(num, string)

the recursion seems to work but I don't get the return value. instead I get

None

help will be appreciated.



Solution 1:[1]

Hi you need to add a return when calling the function recursively. Plus you need to verify what the variable num is at every recursion step, otherwise the recursion won't stop.

Here is the new code :

def has_digit_recursion(num,string):
    num_s = str(num)
    if num_s[-1] == string:
        return True
        
    num //= 10
    if num == 0:
        return False
    return has_digit_recursion(num, string)

print(has_digit_recursion(1234, "0"))
print(has_digit_recursion(1234, "3"))
>>> False
>>> True

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Clément Perroud