Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用于更正电子邮件域的Python函数_Python_Regex_Function - Fatal编程技术网

用于更正电子邮件域的Python函数

用于更正电子邮件域的Python函数,python,regex,function,Python,Regex,Function,好的,我有这个功能构造电子邮件(名称、域): def construct_email(name, domain): if domain == True: print 'True' else: print'None' return name + "@" + domain 这个函数不是很大或者什么的,它应该输出一个电子邮件地址。 但是我还有另一个功能correct\u domain(domain):它假设检查construct\u email

好的,我有这个功能构造电子邮件(名称、域):

def construct_email(name, domain):
    if domain == True:
        print 'True'
    else:
        print'None'
    return name + "@" + domain
这个函数不是很大或者什么的,它应该输出一个电子邮件地址。 但是我还有另一个功能
correct\u domain(domain):
它假设检查
construct\u email(name,domain)中输入的域名:


我的问题是,我如何做到这一点?

如果我理解正确:

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

你说
correct\u domain()
应该检查域名。它有什么作用?给出一些示例输入以及预期和实际输出。假设要确保电子邮件域不包含。在开始或结束时。即。blabla@outlook.com. 而且它不包含2。即blabla@outlook..com电子邮件中至少有1个点I:Eblabla@outlook.comIn
construct\u email
永远不要将变量与布尔值进行比较。只需说
if-domain:
而不是
if-domain==True:
那么调用
correct\u-domain()
时会发生什么呢?实际的问题是什么?怎么了?你需要提供一些例子。好的,谢谢Josh!但是当我运行construct_email(name,domain)并键入My_name=“chris”My_email=construct_email(myname,'outlook.com')时,如何使construct_email(name,domain)调用正确的_domain(domain)谢谢您的回答!这对我帮助很大。我真的弄明白了。def construct\u email(名称,域):if correct\u domain(域):print'True'否则:print'None'返回name+“@”+域导入re def correct\u domain(域):if re.search(r'^\.\124;\.$,domain)或re.search(r'\.\,domain):返回False elif re.search(r'\.,domain):返回True否则:返回False
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...