0

I am having trouble understanding what I have done wrong with the functions I have written for getting user input. I have attached the specific functions and a little test for them.

INT_INPUT_ERROR = 'Please enter only integers'
MENU_SELECTION = 'Make menu selection: '


def stringInput(message):
    userInput = str(input(message))
    if userInput == '':
        stringInput(message)
    else:
        return userInput


def intInput(message):
    userInputAsString = stringInput(message)
    try:
        userInputAsInt = int(userInputAsString)
        return userInputAsInt
    except:
        print(INT_INPUT_ERROR)
        intInput(message)


option = intInput(MENU_SELECTION)

print(option)

If I enter the menu selection which is supposed to be an integer correctly on the first try, the program works as expected - the integer user has input is printed. However, if I input a character, string or an empty input, in the end None is returned. I have looked at the code, but cannot figure out why an integer in the end is not returned if the first value entered is not an integer.

Here is a screenshot of output on my computer - https://1drv.ms/u/s!AguJKhUFrVYFhMp_dqFBfEWD_UbvKg?e=Fhf7It

  • You need to `return` when make your recursive calls or the result is ignored and `None` is returned - `return intInput(message)` / `return stringInput(message)` – Iain Shelvington Mar 11 '22 at 09:50

0 Answers0