-1

好的,我有这个函数construct_email(name, domain):

def construct_email(name, domain):
    if domain == True:
        print 'True'
    else:
        print'None'
    return name + "@" + domain

这个功能并不大,也不是什么,它假设输出一个电子邮件地址。但我也有这个其他功能correct_domain(domain):,假设检查已输入的域名construct_email(name, domain):

import re
def correct_domain(domain):
    if re.search(r'^\.|\.$', domain) or re.search(r'\.\.', domain):
        return False
    elif re.search(r'\.', domain):
        return True
    else:
        return False

我的问题是,我该怎么做?

4

1 回答 1

-2

如果我对您的理解正确:

import re

def construct_email(name, domain):
    if not check_domain(domain):
        return False
    return name + "@" + domain

def check_domain(domain):
    dots = re.findall(r"\.", domain)
    if (len(dots) != 1) or domain.startswith(".") or domain.endswith("."):
        return False
    return True

def main():
    while True:
        email = construct_email(raw_input("Name: "), raw_input("Domain: "))
        if email:
            break
        print "Bad Domain, try again...\n"

    print email
    #other code here...
于 2013-08-15T18:34:26.603 回答