我设置了我的功能我只是不确定如何编写一个elif语句来检查电子邮件是否只有一个域,以及它是否是一个有效的域。
def validEmail(address, domains=(".com ", ".net ", ".org ", ".biz ", ".gov ", ".edu ", ".mil ")):
atLocation = address.index("@")
atCount = address.count("@")
len(address)
periodCount = address.count(".")
periodLocation = address.index(".")
if atLocation <= 0:
return False
elif periodCount <= 0:
return False
elif atCount <= 0:
return False
elif periodLocation ==-1:
return False
else:
return True
print(validEmail("me@hotmail.com"))
print(validEmail("@abc.com"))
print(validEmail("me@abc.fat"))
print(validEmail("me@abc."))
print(validEmail("me@abc.def.com"))
答案 0 :(得分:0)
要获取电子邮件的域名,请使用rsplit:
a_domain = address.rsplit('.', 1)[1]
要检查a_doimain是否在您的域列表中,您可以执行以下操作:
valid_domian = a_domain in domains