[Tutor] email validation

Quiles, Stephanie stephanie.quiles001 at albright.edu
Sun Aug 2 03:55:49 CEST 2015


> 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
        email2 = input("Enter alternate email address :")
        if '@' not in email2 or '.' not in email2:
            print('email needs @ and . at the same time')
            cont = False
        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

    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...
        outfile = open("emails.dat", "wb")
        pickle.dump(emails, outfile)
        outfile.close
        print("Your data has been saved to emails.dat")


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()


This is the output: 

/Library/Frameworks/Python.framework/Versions/3.4/bin/python3.4 /Users/stephaniequiles/Downloads/emailsupdate.py
{'maria': ['steph', 'sst', '33ed'], 'Jim': 'ththth at ththt', 'Ton': 'tomtomtomt at tomtom', 'Bob': 'bob at bob.com'}
Enter your name :sfdgh
Enter your email address :sdfkjsdf at syesgd.com
Enter alternate email address :sdfghfds at Asfdgfdcod
email needs @ and . at the same time
Enter your phone number :

I must have something missing but can’t remember what it is. Thanks for your help! 

Stephanie 

            
> On Sat, Aug 1, 2015 at 2:03 PM, Válas Péter <turtle at 64.hu> wrote:
>> Hi Stephanie,
>> 
>> the function should be defined first, and used after. So put it before
>> main().
> 
> 
> 
> It's perfectly legal and ok to say:
> 
> ###########################
> def main():
>     callHelper()
> 
> def callHelper():
>     print("I am the helper")
> 
> main()
> ###########################
> 
> 
> 
> Rather, the problem is due to putting the helper function accidentally
> nested *within* main:
> 
> ############################
> def main():
>     callHelper()
> 
>    def callHelper():
>         print("I am the helper but can't be called until after the definition")
> 
> main()
> #############################
> 
> 
> 
> One technical way to "fix" this is to move it up a bit:
> 
> #############################
> def main():
>    def callHelper():
>         print("I am the helper but can't be called until after the definition")
> 
>    callHelper()
> 
> main()
> #############################
> 
> 
> 
> But this is usually unsatisfactory because we can't then access
> callHelper from outside.  There can be valid reasons to hide function
> definitions at times, but this isn't one of those situations.
> 
> 
> Válas's suggestion, to move the helper's definition above, does make sense:
> 
> #############################
> def callHelper():
>     print("I am the helper but can't be called until after the definition")
> 
> def main():
>    callHelper()
> 
> main()
> #############################
> 
> but a key point needs to be made: don't just move it *up*, but move it *out*.



More information about the Tutor mailing list