A20392. 验证邮箱小明用Python语言写了一个验证邮箱地址和密码是否符合要求的程序。邮箱地址,例如1a2b3c4d56@sina.com,“@”前面的字段叫用户名,“@”后面的字段叫域名。小明的验证程序要求邮箱地址长度不小于15位,同时用户名包含字母和数字。邮箱密码的长度不小于8位,要同时包含大写字母、小写字母以及数字。下面是小明写的程序代码,请你补全横线处的代码,实现前面描述的功能。#验证邮箱地址 e…
填空题
困难
知识点
题目描述
验证邮箱
小明用Python语言写了一个验证邮箱地址和密码是否符合要求的程序。邮箱地址,例如1a2b3c4d56@sina.com,“@”前面的字段叫用户名,“@”后面的字段叫域名。小明的验证程序要求邮箱地址长度不小于15位,同时用户名包含字母和数字。邮箱密码的长度不小于8位,要同时包含大写字母、小写字母以及数字。下面是小明写的程序代码,请你补全横线处的代码,实现前面描述的功能。
#验证邮箱地址
email = input("请输入邮箱地址: ")
DiZhi = False
if ① and '@' in email and '.' in email:
YongHuMing, YuMing = ②
if YongHuMing and YuMing and '.' in YuMing:
if any(char.isdigit() for char in YongHuMing) and any( ③ for char in YongHuMing):
DiZhi = True
# 验证密码
password = input("请输入密码: ")
MiMa = False
if len(password) >= 8:
DaXie = any(char.isupper() for char in password)
XiaoXie = any(char.islower() for char in password)
ShuZi = any(char.isdigit() for char in password)
if DaXie and XiaoXie and ShuZi:
MiMa = True
# 输出结果
if ④ :
print("邮箱地址和密码格式均正确。")
else:
if not DiZhi:
print("邮箱地址格式不正确。")
if not MiMa:
print("密码格式不正确。")参考答案
# 验证邮箱地址
email = input("请输入邮箱地址: ")
DiZhi = False
#①字符串的长度,邮箱地址长度不小于15位
if len(email) >= 15 and '@' in email and '.' in email:
YongHuMing, YuMing = email.split('@') #②分割字符串,把邮箱地址用“@”分割成用户名和域名
if YongHuMing and YuMing and '.' in YuMing:
#③字符串的常用方法,判断是否含有字母
if any(char.isdigit() for char in YongHuMing) and any(char.isalpha() for char in YongHuMing):
DiZhi = True
# 验证密码
password = input("请输入密码: ")
MiMa = False
if len(password) >= 8:
DaXie = any(char.isupper() for char in password)
XiaoXie = any(char.islower() for char in password)
ShuZi = any(char.isdigit() for char in password)
if DaXie and XiaoXie and ShuZi:
MiMa = True
# 输出结果
#④字符串不为空时,逻辑值为True。逻辑运算,两个条件同时满足进入分支语句。
if DiZhi and MiMa:
print("邮箱地址和密码格式均正确。")
else:
if not DiZhi:
print("邮箱地址格式不正确。")
if not MiMa:
print("密码格式不正确。")
上一题
下一题