-1

def checkLuckyDigit(num):
    if num == 0:
        return False
    elif (num%10) == 8:
        return True
    else:
        checkLuckyDigit(num//10)

num = int(input("Enter a number = "))

if(checkLuckyDigit(num)):
    print("There is a lucky digit")
else:
    print("There is no lucky digit")

In the above code i need to find out whether the user entered number has 8 in it or not , when i enter 4348 it displays There is a lucky digit but when i enter 3438434 it displays There is no lucky digit and the function returns None.

I recently moved from C++ to python so i am really confused what my mistake is.

RNGesus.exe
  • 205
  • 1
  • 12

2 Answers2

5

If a Python function reaches the end with no return, it implicitly returns None, which is what happens when you hit your recursive call. You just need to change the last line to return checkLuckyDigit(num//10)

Randy
  • 14,349
  • 2
  • 36
  • 42
0

You didn't handle the unwinding condition Do it like this

def checkLuckyDigit(num):
    if num > 0:
        if num % 10 == 8:
            return True
        if checkLuckyDigit(num//10): #This checks the unwinding condition
                return True
    else:
        return False

num = int(input("Enter a number ="))


if checkLuckyDigit(num):
    print("There is a lucky digit")
else:
    print("There is no lucky digit")