gpt4 book ai didi

python - 使用正则表达式时如何连接多个 if 语句?

转载 作者:太空宇宙 更新时间:2023-11-04 10:09:49 25 4
gpt4 key购买 nike

下面是至少一个小写,一个大写和一个数字的密码验证过程的代码。

import re
password=raw_input('Enter the Password')

x= (re.findall(r'[a-z]',password))
if len(x)==0:
print " at least one 'a-z' requirment not completed"
else:
#(what should i write here to connect this to the next step?)

y=(re.findall(r'[A-Z',password))
if len(y)==0:
print "at least one 'A-Z' requirement not completed"
else:
#(what should i write here?)

z=(re.findall(r'[0-9]',password))

if len(z)==0:
print "at least one '0-9' requirment not completed"
else:
print ' Good password!'

我的愿望是让 Python 以这样一种方式运行:如果 x 不为 0,则继续验证 y 是否等于 0。如果不等于,验证 z 不等于 0。如果不等于,告诉用户密码正确。

最佳答案

使用if-elif语句:

import re
password=raw_input('Enter the Password')

if len(re.findall(r'[a-z]',password))==0:
print " at least one 'a-z' requirment not completed"
elif len(re.findall(r'[A-Z]',password)) == 0:
print "at least one 'A-Z' requirement not completed"
elif len(re.findall(r'[0-9]',password)) == 0:
print "at least one '0-9' requirment not completed"
else:
print ' Good password!'

演示:

Enter the Password  Axa3
Good password!

Enter the Password aa3
at least one 'A-Z' requirement not completed

Enter the Password A3443E
at least one 'a-z' requirment not completed

关于python - 使用正则表达式时如何连接多个 if 语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39059664/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com