[Tutor] email validation
Alan Gauld
alan.gauld at btinternet.com
Sun Aug 2 10:31:58 CEST 2015
On 02/08/15 02:55, Quiles, Stephanie wrote:
>> On Aug 1, 2015, at 5:17 PM, Danny Yoo <dyoo at hashcollision.org> wrote:
>> Thank you, the program is now working but when the email is not entered
>> correctly it doesn’t make me go back and re-enter,
>> it spits out an error code but then moves on to the next field .
>
> Here is the code:
>
> import pickle
>
>
> def main():
> cont = True
> emails = open_existing_file()
> print(emails)
>
> # Get data...
> while cont:
> name = input("Enter your name :")
> email1 = input("Enter your email address :")
> if '@' not in email1 or '.' not in email1:
> print('email needs @ and . at the same time')
> cont = False
Instead of setting cont here - which will not stop the loop
from continuing (the while only checks the condition at the
start of each iteration) you should use continue. continue
jumps straight back to the top of the loop for another
iteration. I thnk that is what you want.
...
> c = input("Enter another? [y]/n :")
> if c == 'n' or c == 'N':
> cont = False
>
> def email1():
> if '@' not in email1 or '.' not in email1:
> print('email needs @ and . at the same time')
>
> def email2():
> if '@' not in email2 or '.' not in email2:
> print('email needs @ and . at the same time')
> # Save data...
These are no longer being used. instead you create variables of the same
name which these then replace with functions. That is a recipe for
confusion. You should remove these functions. Either that or move
them outside the main block and use them in your tests. In that case you
only need one function which I'd call something like test_email()
def is_good_address(addr):
if '@' not in addr or '.' not in addr:
print('email needs @ and . at the same time')
return False
else: return True
You can then use it like
if not is_good_address(email2): continue
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list