2016-07-21 80 views
0
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

你可能想看看[這裏](HTTP ://stackoverflow.com/q/20002503/198633) – inspectorG4dget

+0

請在您的代碼中添加說明。 –

+0

'或'不能這樣工作;你的表達式實際上是「A」或「B」或(「C」或密碼中的「Z」))「Python將會去」「A」計算結果爲True,所以第一個或「是真的,不需要檢查剩下的!」,因此你的表情總是會評估爲真。爲了你的代碼的工作,你需要'如果「密碼」中的「A」或密碼或「...」中的「B」(這是一種不好的方法,顯然 - 答案會給出好的方法)。 – marcelm

回答

3

試試這個:

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

+0

感謝一堆! –

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 
相關問題