1. 程式人生 > >python-校驗密碼小練習

python-校驗密碼小練習

#校驗密碼是否合法的小練習
#1、密碼長度5到10位;
#2、密碼裡面必須包含,大寫字母,小寫字母,數字
#3、最多輸入5次

寫程式過程中遇到了兩個問題,第二個迴圈裡的P是把password的值迴圈傳到p裡面去;例如密碼輸入'123abcABC';是迴圈從1開始依次取字串裡的值;第二個問題:
p.isupper寫錯誤,導致numissupper值一直為0;所以一直走不到密碼校驗通過


for i in range(10):
password=input('請輸入你的密碼:').strip()
if len(password)>4 and len(password)<10:
numlower = 0
numisupper = 0
numdigit = 0
for p in password:
if p.islower():
numlower+=1
elif p.isupper():
numisupper+=1
elif p.isdigit():
numdigit+=1
if numlower > 0 and numdigit > 0 and numisupper > 0:
print('密碼校驗通過')
break
else:
print('密碼不符合要求')
else:
print('長度不符合要求')

# for i in range(10):
# password = input('輸入密碼:')
# #print(password.islower()) #僅小寫字母=T,不包含就為假;數字+小寫字母=T 小寫字母+大寫字母=False
# #print(password.isupper()) # 僅大寫字母=T,不包含就為假;數字+大寫字母=T 小寫字母+大寫字母=False
# #print(password.isdigit()) #僅有數字組成