有没有更简单的方法来检查密码是否包含大写字母

时间:2016-07-21 21:05:22

标签: python-3.x

Username = input("please enter a username: ")
Password = input("please enter a password: ")

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

2 个答案:

答案 0 :(得分:3)

试试这个:

if Password != Password.lower() print("yes")

答案 1 :(得分:1)

str.isupper()告诉您字符串是否为大写字母。您可以使用它来测试每个角色是否都是大写的。循环遍及整个密码字符串,您可以检查是否有任何字符是大写的

In [3]: p = input("Please enter a password: ")
Please enter a password: asfF

In [4]: any(char.isupper() for char in p)
Out[4]: True

In [5]: p = input("Please enter a password: ")
Please enter a password: asdf

In [6]: any(char.isupper() for char in p)
Out[6]: False