[Tutor] email validation

Quiles, Stephanie stephanie.quiles001 at albright.edu
Sat Aug 1 19:54:17 CEST 2015


Hello All, 

I have a python assignment. I have to make sure that when user inputs email that the program verifies that the address as a @ and a “.” in the entry or else return an invalid email error. 
A Very rudimentary form of email validation. i cannot get the program to work. Here is what i have so far: 

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 :")
        email2 = input("Enter alternate email address :")
        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 email and '.' not in email:
                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()

Here is the error message : 

/Library/Frameworks/Python.framework/Versions/3.4/bin/python3.4 /Users/stephaniequiles/Downloads/emailsupdate.py
Traceback (most recent call last):
  File "/Users/stephaniequiles/Downloads/emailsupdate.py", line 42, in <module>
    main()
  File "/Users/stephaniequiles/Downloads/emailsupdate.py", line 6, in main
    emails = open_existing_file()
UnboundLocalError: local variable 'open_existing_file' referenced before assignment

Process finished with exit code 1

not sure why it is not recognizing that the open_existing_file() function needs to be returned to the “emails” variable? I am guessing it has to do with my syntax? any suggestions, please? 

Thank you 

Stephanie Quiles 

            



More information about the Tutor mailing list