tags:

views:

176

answers:

5

I'm trying to figure out the problem in this short paragraph of code. Any help would be appreciated. Regardless of what I specify User.email to be, it always returns false.

def add(self):

    #1 -- VALIDATE EMAIL ADDRESS
    #Check that e-mail has been completed
    try:
        #Validate if e-mail address is in correct format
        if (isAddressValid(self.email) == 0):
            self.errors['email'] = 'You have entered an invalid e-mail address';
            return 0

    except NameError:
        self.errors['email'] = 'Please enter your e-mail'
        return 0

>>> u = User()
>>> u.email = '[email protected]'
>>> u.add()
0
>>> print u.errors
{'email': 'Please enter your e-mail'}

I have confirmed that the false being returned is coming from except NameError.

Also, isAddressValid() is just a method to check the structure of an e-mail address.

Thanks.

+1  A: 

Actually you have two values.

  • 0
  • None

If you print the value instead of using it in an if-statement, you'll see the two conditions. Consider adding print statements to see what the value actually is.

if (isAddressValid(self.email) == 0):

If this is True, you get 0.

If this is False, you'll get None.

And the exception give 0.

S.Lott
What do you mean? thanks.
ensnare
@ensnare: I mean ADD PRINT STATEMENTS. See what the values are. I mean that your if-statement is incomplete. If you ADD PRINT STATEMENTS, you'll see how it's incomplete.
S.Lott
Got it. Thanks.
ensnare
+2  A: 

You haven't included a return statement for the positive case... Also, when a function doesn't include a return statement, the caller receives None instead...

def add(self):

    #1 -- VALIDATE EMAIL ADDRESS
    #Check that e-mail has been completed
    try:
        #Validate if e-mail address is in correct format
        if (isAddressValid(self.email) == 0):
            self.errors['email'] = 'You have entered an invalid e-mail address';
            return False

    except NameError:
        self.errors['email'] = 'Please enter your e-mail'
        return False

    return True
jldupont
A: 

Im not sure what problem you are talking about , but you are always returning 0

Try adding an else clause for the case of the Valid Email (which you are currently not considering)

def add(self):

#1 -- VALIDATE EMAIL ADDRESS
#Check that e-mail has been completed
try:
    #Validate if e-mail address is in correct format
    if (isAddressValid(self.email) == 0):
        self.errors['email'] = 'You have entered an invalid e-mail address';
        return 0
    else
        return 1

except NameError:
    self.errors['email'] = 'Please enter your e-mail'
    return 0
Tom
A: 

You said isAddressValid is a method, right? Since add is also a method, perhaps you have to prepend a self.:

if (self.isAddressValid(self.email) == 0):

This most probably will deal with your NameError.

After that, add an else clause when the check succeeds:

    …
    self.errors['email'] = 'You have entered an invalid e-mail address'
    return 0
else:
    return 1
ΤΖΩΤΖΙΟΥ
+1  A: 

If I were re-writing this code, I would go for something like this:

def add(self):
    try:
        if not isAddressValid(self.email):
            self.errors['email'] = 'You have entered an invalid e-mail address';
    except NameError:
        self.errors['email'] = 'Please enter your e-mail'
    return 'email' not in self.errors
hughdbrown