[Tutor] email validation

Quiles, Stephanie stephanie.quiles001 at albright.edu
Mon Aug 3 05:02:20 CEST 2015


ok so i made a few corrections based on what you stated and moved the name variable out of the loop. however, now its doing the same thing with alternate email. if it is entered incorrectly, it goes back to email 1 and you have to re-enter it. i tried the nested loops but they were not working either. where should i insert them into the program to make it work? 


> On Aug 2, 2015, at 6:54 PM, Quiles, Stephanie <stephanie.quiles001 at albright.edu> wrote:
> 
> So i took your advice and i am much closer. however, when i type in an invalid address it loops back to the first prompt and asks you to enter your name: I want it to ask you to re-enter your email address instead how would i go about this?
> 
> Here is the corrected 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 is_good_address(email1): continue
>        email2 = input("Enter an alternate email address: ")
>        if not is_good_address(email2): continue
>        phone = input("Enter your phone number: ")
>        contactlist = [email1, email2, phone]
>        emails[name] = contactlist
>        c = input("Enter another? [y]/n: ")
>        if c == 'n' or c == 'N':
>            cont = False
> 
>            # Save data...
>        outfile = open("emails.dat", "wb")
>        pickle.dump(emails, outfile)
>        outfile.close
>        print("Your data has been saved to emails.dat")
> 
> 
> 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
> 
> 
> def open_existing_file():
>    # returns an empty dictionary or one that has data from a file
>    emails = {}
>    # Load the dictionary
>    try:
>        infile = open("emails.dat", "rb")
>        emails = pickle.load(infile)
>        infile.close()
>    except:
>        print("No file to open. Starting with no data.")
>    return emails
> 
> 
> main()
> 
> 
> Thanks 
> 
> Stephanie 
> 
> 
>> On Aug 2, 2015, at 10:12 AM, Alan Gauld <alan.gauld at btinternet.com> wrote:
>> 
>> On 02/08/15 09:31, Alan Gauld wrote:
>> 
>>> 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()
>> 
>> Ahem. Or you could call it is_good_address() of course! Oops!
>> 
>>> 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
>> 
>> -- 
>> 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
>> 
>> 
>> _______________________________________________
>> Tutor maillist  -  Tutor at python.org
>> To unsubscribe or change subscription options:
>> https://mail.python.org/mailman/listinfo/tutor
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list