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.

5
  • 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. Commented Feb 27, 2023 at 8:32
  • @Shorn so would an empty input be detected by .isdigit()? Commented Feb 27, 2023 at 8: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. Commented Feb 27, 2023 at 9: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. Commented Feb 27, 2023 at 9: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. Commented Feb 28, 2023 at 0:58

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.