Python中为何‘and not’逻辑无法实现密码需大写和数字的校验?
Why Your "and not" Logic Isn't Triggering Correctly
Hey there! Let's walk through exactly why your current condition isn't catching the cases where the password has a capital letter but no number, or a number but no capital letter.
First, let's look at your conditional line again:
if password.islower() and "1234567890" not in password:
Let's break down each part of this condition and how they interact:
password.islower(): This checks if every single character in the password is lowercase. If there's even one uppercase letter present, this returnsFalseright away."1234567890" not in password: This checks if the password contains no digits at all. If any digit exists in the password, this returnsFalse.
Now let's map this to your problematic scenarios:
- Password has a capital letter but no number:
password.islower()becomesFalse(thanks to the uppercase letter), so the entireandcondition fails (sinceandrequires both parts to beTrue). That's why the error message doesn't show up here.
- Password has a number but no capital letter:
password.islower()isTrue(all characters are lowercase), but"1234567890" not in passwordbecomesFalse(since there's a digit). Again, theandcondition needs both parts to beTrueto trigger, so the error message stays hidden.
Only when both the password is entirely lowercase and has no digits does the condition evaluate to True—which is why that's the only case your error message appears.
内容的提问来源于stack exchange,提问作者Anderdoodle
相关产品推荐
相关产品推荐

