0
def inp():
    try:
        k=input("enter")
        l=int(k)
        return l
    except :
        inp()

print(inp())
enterll
enter6
None

Process finished with exit code 0
Unmitigated
  • 76,500
  • 11
  • 62
  • 80

2 Answers2

1

You need to return the result of the recursive call in the except block; otherwise, None is implicitly returned as you did not explicitly return anything.

def inp():
    try:
        k = input("enter")
        l = int(k)
        return l
    except ValueError:
        return inp()
Unmitigated
  • 76,500
  • 11
  • 62
  • 80
0

After handling the exception, there is not return value. This is the problem when you have multiple returns. You can fix it with one return

def inp():
    try:
        k=input("enter")
        l=int(k)
    except :
        l = inp()
    return l

or add a return in the exception handler

def inp():
    try:
        k=input("enter")
        l=int(k)
        return l
    except :
        return inp()
cup
  • 7,589
  • 4
  • 19
  • 42