开始使用免费开始使用

无效密码

网站项目的第二部分是编写一个脚本,用来验证用户输入的密码是否有效。 公司还规定了验证有效密码的规则:

  • 可以包含小写字母 a-z 和大写字母 A-Z
  • 可以包含数字
  • 可以包含以下符号:*#$%!&.
  • 长度至少为 8 个字符,但不超过 20 个字符

您的同事还给了您一份密码列表,作为测试示例。

列表 passwords 和模块 re 已加载到您的会话中。您可以使用 print(passwords) 在 IPython Shell 中查看。

本练习是课程的一部分

Python 中的正则表达式

查看课程

练习说明

  • 编写一个正则表达式,根据以上描述检查密码是否有效。
  • passwords 列表中逐一搜索,判断是否为有效密码。
  • 为了打印出是否为有效密码的提示信息,请补全 .format() 语句。

交互式实操练习

通过完成这段示例代码来试试这个练习。

# Write a regex to check if the password is valid
regex = ____"[____]____"

for example in passwords:
  	# Scan the strings to find a match
    if re.____(____, ____):
        # Complete the format method to print out the result
      	print("The password {____} is a valid password".format(pass_example=____))
    else:
      	print("The password {____} is invalid".format(pass_example=____))   
编辑并运行代码