Вопрос

I am making algorithm for checking the string (e-mail) - like "E-mail addres is valid" but their are rules. First part of e-mail has to be string that has 1-8 characters (can contain alphabet, numbers, underscore [ _ ]...all the parts that e-mail contains) and after @ the second part of e-mail has to have string that has 1-12 characters (also containing all legal expressions) and it has to end with top level domain .com

email = raw_input ("Enter the e-mail address:")
length = len (email)
if length > 20 
    print "Address is too long"
elif lenght < 7:
    print "Address is too short"  
if not email.endswith (".com"):   
    print "Address doesn't contain correct domain ending"   
try:
    first_part = len (splitting[0])
    second_part = len(splitting[1])  

    account = splitting[0]
    domain = splitting[1] 

    list = "abcdefghijklmopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_."

    for c in account: 
        if c not in list:
            print "Invalid char", "->", c,"<-", "in account name of e-mail"

    for c in domain:
        if c not in list:
            print "Invalid char", "->", c,"<-", "in domain name of  e-mail"

    if first_part == 0:
        print "You need at least 1 character before the @"
    elif first_part> 8:
        print "The first part is too long"
    if second_part == 4:
        print "You need at least 1 character after the @"
    elif second_part> 16:
        print "The second part is too long"
except IndexError:
        print ""The address must consist of 2 parts connected with symbol @,\
 and ending must be .com"

    if first_part > 0 and first_part < 8 and second_part >4 and second_part < 16:
       print "Valid e-mail address"
Это было полезно?

Решение

If I understood well, you got everything working, except the part of finding invalid chars. Is that true?

Do you know the for loop? It may be helpful to you. Just get the parts of the e-mail:

account = splitting[0]
domain = splitting[1]

Then, iterate over each part. It will yield a character each time. If this char is not in the set of allowed ones, you print a message:

for c in account:
    if c not in "abcdefghijklmopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_.":
        print "Invalid char", c, "in e-mail"

for c in domain:
    if c not in "abcdefghijklmopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_.":
        print "Invalid char", c, "in e-mail"

This is not a very elegant solution (one could use string.ascii_letters+string.digits+"._" instead of "abcdefghijklmopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_.", for example, or list comprehesions), but I bet it is sufficiently understandable for a new user.

Другие советы

Regular expressions FTW!

import re

address = 'test@gmail.com'
if re.match(r'^[a-z0-9_]{1,8}@[a-z0-9_]{1,8}\.com$', address, re.IGNORECASE):
  print 'valid'
else:
  print 'invalid'

A shorter regular expression (as the comments note) would be r'^\w{1,8}@\w{1,8}\.com$'

I don't know if that's what your teacher is aiming for, but regex are always good to know :)

Construct a validate email method, with two parameters the email address to validate and a list of valid domains.

def validEmail(email, domains):

    length = len(email)

    index = email.find('@')

    if len(email[0:index]) <= 8 and len(email[0:index]) > 0:
        dot = email.find('.')
        if (len(email[index + 1:]) - dot) <= 12:
            if index+1==dot:
                return False
            else:
                for i in domains:
                    if email[dot + 1:] == i:
                        return True
    return False

domains = ['com', 'org', 'co.uk']

email = raw_input ("Enter the e-mail address:")
print validEmail(email, domains)
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top