[Tutor] email validation

Alan Gauld alan.gauld at btinternet.com
Mon Aug 3 01:19:24 CEST 2015


On 02/08/15 23:54, Quiles, Stephanie 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?

Just move the line that asks for the name above the while loop.
Then it will only ask for the name once.

If you want to cater for multiple users each with multiple addresses then
you will need two nested loops.

while True
     user = input(...name...):
     collect other user data here
     while True:
            get/check addresses here

>      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

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