在列表,输入中寻找事物

时间:2018-06-25 13:41:13

标签: python list input

我目前正在使用Python 3.3.2进行密码检查,目前正在通过以下操作在用户输入中查找内容:

if ("A" in PassCheck) or ("B" in PassCheck) or ("C" in PassCheck) or ("D")in 
PassCheck) or ("E" in PassCheck) or ("F" in PassCheck) or ("G" in PassCheck) 
or ("H" in PassCheck) or ("I" in PassCheck) or ("J" in PassCheck) or ("K" in 
PassCheck) or ("L" in PassCheck) or ("M" in PassCheck) or ("N" in PassCheck 
 or
("O" in PassCheck) or ("P" in PassCheck) or ("Q" in PassCheck) or ("R" in 
PassCheck) or ("S" in PassCheck) or ("T" in PassCheck) or ("U" in PassCheck)
or ("V" in PassCheck) or ("W" in PassCheck) or ("X" in PassCheck) or ("Y" in 
PassCheck) or ("Z" in PassCheck):

有没有办法用列表做到这一点?

1 个答案:

答案 0 :(得分:2)

您可以使用string模块使用string.ascii_uppercase库来搜索大写字符的存在情况

>>> import string
>>> string.ascii_uppercase
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

然后您可以使用any函数来测试给定字符串输入中是否存在大写字符。

>>> Pass_Check_1 = 'ThisOneWorks'
>>> any(i in Pass_Check_1 for i in string.ascii_uppercase)
True

>>> Pass_Check_2 = 'thisonedoesntwork'
>>> any(i in Pass_Check_2 for i in string.ascii_uppercase)
False