0

I was wondering if it was possible to have an input variable look for an integer input but also be able to detect if the user leaves no input. I was wanting to make a simple app where the user chooses an option out of a numbered list and want the program to default to the first option if the user leaves the input area blank.

I currently have a try loop that continually asks the user for an integer as an input written like this:

def inputNum():
    while True:
        try:
            userInput = int(input())
        except ValueError:
            print("Input is not an integer");
            continue
        except EOFError:
            return ""
            break
        else:
            return userInput
            break


selection = inputNum()

I have a feeling part of this is blatantly wrong, but I hope everyone gets the gist of things and can help provide helpful feedback and answers.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
mxrgan
  • 11
  • 3
  • if you don't immediately cast `userInput` to `int`, you can check if it is blank with `userInput == ""`. Similarly, you can check if it is an integer with string's `.isdigit()`, so `userInput.isdigit()`. Doing the try/catch the way you have also works for checking if it is an integer, although it may lead to unexpected effects. – Shorn Feb 27 '23 at 08:32
  • @Shorn so would an empty input be detected by `.isdigit()`? – mxrgan Feb 27 '23 at 08:43
  • Aside from that: the main problem here is that an empty input **will not** cause `EOFError` on `int`. It causes `EOFError` *when the string will be interpreted as Python code*, for example using `eval` or `exec` (**do not use such tools here**). Once you understand how to check for an empty string, there isn't a meaningful question any more; so I closed this as a duplicate of the canonical for how to check for an empty string. – Karl Knechtel Feb 27 '23 at 09:08
  • Okay, I have just checked earlier on this topic and found EOFError as a method for that, so thanks for giving me more understanding about that. I do believe this is different enough to not be a dupe but I did get a good answer and some decent info from others so I feel good about that. HAve a nice day/night yall. – mxrgan Feb 27 '23 at 09:11
  • @mxrgan `"".isdigit()` returns `False` if that's what you're asking. If you want to specifically check if a string is empty, doing `userInput == ""` would do that. – Shorn Feb 28 '23 at 00:58

1 Answers1

0

Don't convert to integer while requesting input. Split your input request into 2 steps:

def inputNum():
    while True:
        userInput = input().strip()
        if not userInput:
            print('Input is empty')
            continue
        try:
            userInput = int(userInput)
        except ValueError:
            print("Input is not an integer");
            continue
        return userInput

selection = inputNum()

Example:

 
Input is empty
 abc
Input is not an integer
 123

Output: 123

mozway
  • 194,879
  • 13
  • 39
  • 75