【发布时间】:2013-12-03 05:21:06
【问题描述】:
我正在编写一个程序,它应该接受一个字符串(用户输入的密码)并对其进行测试以确保它满足以下要求: 必须至少包含一个大写字母和一个小写字母 必须以字母开头 最少八个字符 没有空格 必须至少包含两位数
这是我目前所拥有的,但我遇到了无效的语法错误 运行 = 真
while running:
valid = 0
password = str("Enter Password: ")
if len(p) <8:
print ("The password you entered is too short. Please try again.")
running = False
import re
#check if contains a digit
if re.search(r '\d', password):
password = valid
#check if contains uppercase letter
if re.search(r '[A-Z]', password):
password = valid
#check if contains lowercase letter
if re.search(r '[a-z]', password):
password = valid
#check if contains only letters
if re.search(r "[a-z]", password) and re.search(r "[A-Z]", password):
print ("Password must contain at least 2 digits. Please try again.")
#check if contains all lowercase letters
if password.islower():
print ("Password must contain at least 1 uppercase letter. Please try again.")
#check if contains all uppercase letters
if password.isupper():
print ("Password must contain at least 1 lowercase letter. Please try again.")
if password == valid:
print ("Valid Password")
【问题讨论】:
-
这里的逻辑根本不起作用——一旦你通过了第一个(数字)检查,你设置的密码等于“有效”(它是零..)。然后你将失败所有其他测试(实际上我认为抛出一个错误,因为 re.search 需要一个字符串。
-
你认为“制作密码检查器”是每本 Python 教科书第 2 章中的家庭作业练习 1..
标签: python