Search code examples
python-3.xisnumericisalpha

Iteratively testing for an input that is numeric in Python3


I have to test if user's input is numeric. Otherwise, I need to ask for input again. It sounds simple, right? But, check the following function

# Request number from user
def request_number():
    number = input("Please, enter the card number you want to check: ")
    if number.isnumeric():
        return int(number)
    elif number.isalpha() or number.isalnum() or number.isdecimal():
        request_number()

It Works fine if the first input from the user is actually a number 4111111111111111

But, if the user inputs first a string, such as asbsdl. The function correctly asks a second time for a number, but even when you enter a number, it returns None.

Can anybody shed some light? Thanks.


Solution

  • so the reason is because you aren't returning your recursive call. To fix this, change the recursion call (last line) to return request_number()