這可以縮短/改進嗎?我試圖在python中創建一個密碼檢查器。可以縮短或改進此代碼嗎?
if可以放在for循環中嗎?如果是這樣,怎麼樣?
pw = input("Enter password to test: ")
caps = sum(1 for c in pw if c.isupper())
lower = sum(1 for c in pw if c.islower())
nums = sum(1 for c in pw if c.isnumeric())
scr = ['weak', 'medium', 'strong']
r = [caps, lower, nums]
if len(pw) < 6:
print("too short")
elif len(pw) > 12:
print("too long")
if caps >= 1:
if lower >= 1:
if nums >= 1:
print(scr[2])
elif nums < 1:
print("your password is " + scr[1])
elif lower < 1:
print("your password strength is " + scr[0])
elif caps < 1:
print("your password strength is " + scr[1])
感謝您的任何建議:d
codereview.stackexchange.com是應該在哪裏。這兩個問題的答案我確定是的,它可以縮短和改善 –
'scr [1]'絕對不比''medium'更簡單或更有意義。 (如果你想讓你的字符串本地化......有更好的方法來做到這一點。) – abarnert
這個問題似乎是脫離主題,因爲它是關於審查工作代碼。請嘗試[代碼評論](http://codereview.stackexchange.com)。 – abarnert