day13练习

"""
5.1 验证电子邮件字符串是否合法
要求:
1. @之前不能包含(中杠)-,及其他$&等符号,但可以包含., 开头字母和数字
2. 统一命名is_valid_email()
3. @之后数字或者字母
 后缀.com|.gov|.net任一结尾
参考:测试其合法性
1. [email protected]                     真
2. [email protected]                真
3. [email protected]                      真
4. [email protected]        假
5. [email protected]                      真
"""
import math
import re

def email_path_before_chack(str):
if not re.compile("^[a-zA-Z0-9][a-zA-Z0-9,'.']*$").match(str):
print("邮箱地址输入错误")
return False
return True

def email_path_last_chack(str):
if not re.compile("^[a-zA-Z0-9,'.']*$").match(str.split(".")[0]):
print("邮箱地址后缀名输入错误")
return False

if not str.split(".")[1] in ["com","gov","net"]:
print("邮箱网站地址输入错误")
return False
return True

def is_valid_email(email_path):
if "@" not in email_path:
print("该地址格式不正确,没有@")
return
if not email_path_before_chack(email_path.split("@")[0]):
return
if not email_path_last_chack(email_path.split("@")[1]):
return
print("{}是合法邮箱地址".format(email_path))
return
def main():
while True:
email_path = input("请输入邮箱地址:\n")
if email_path == "退出":
break
is_valid_email(email_path)
if __name__ == "__main__" :
main()


程序执行结果为

 



猜你喜欢

转载自www.cnblogs.com/lyy17759893807/p/12046307.html