3

The question kind of explains it. I was thinking it would be something like:

x = None
if sUserInt == x:
    #do stuff here

I'm using it to check if a user has just pushed enter and put nothing in at all. Thanks!

1
  • 1
    Your input won't be nothing; it'll be an empty string. That may seem like nothing, but they're as different as 1 and '1'. Commented Dec 15, 2013 at 12:50

2 Answers 2

3

First, you don't have to define a new variable only to compare its value. So instead of

x = None
if sUserInt == x:
    #do stuff here

You could write

if sUserInt == None:
    #do stuff here

However, in case of None, you can use the implicit checking of a variable being None:

if not sUserInt:
    #do stuff here

And finally, all these won't help you anyway, because simply pushing an enter on raw_input() won't give you None, but an empty string ('').

So use:

if sUserInt == '':
        #do stuff here

or check the length of input

if len(sUserInt) == 0:
        #do stuff here

or use the implicit string checking again:

if not sUserInt:
        #do stuff here

All the last three solutions mean basically the same thing.

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

1 Comment

You used the implicit false for None, but not ''?
2

Rather Use :

if not sUserInt:
    #do something here

Example usage:

sUserInt = raw_input()
if not sUserInt:
    print "Nothing Entered"

If nothing was entered it would print Nothing Entered

1 Comment

Note that sUserInt is an empty string ('') and not None in this case. However, implicit checking of emptyness still works for strings.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.