Consider this code:
>>> num = int(raw_input('Enter the number > '))
If the user types nothing and presses ‘Enter’, I want to capture that. (Capture an empty input)
There are two ways of doing it:
- I do a simple
num = raw_input(), then check whethernum == ''. Afterwards, I can cast it to aint. - I catch a
ValueError. But in that case, I can’t differentiate between an non-numerical input and a empty input.
Any suggestions on how to do this?
Something like this?
This relies on the fact that
int()applied to a number will simply return that number, and that the emtpy string evaluates toFalse.