Is there a way in Python to continue checking conditions of an if else statement if one evaluates to true? Here's my code:
status = True
if pass_len(S) == False:
print ('Password must be at least 6 characters long')
status = False
elif pass_upper(S) == False:
print('Password must include upper case letters')
status = False
elif pass_lower(S) == False:
print('Password must include lower case letters')
status = False
elif pass_nums(S) == False:
print('Password must include digits.')
status = False
else:
status = True
return status
For example, if the password doesn't have an uppercase character or numbers, I'd like to print both messages, instead of just "Password must include upper case letters" and it ending right there. I tried getting around this by taking return out of each statement, but it didn't work. Any help is appreciated, thanks.
解决方案
Don't use elif, using if will make sure the the condition is checked. elif will only be checked when the if statement evaluates to False
In [40]: foo = 3
In [41]: if foo == 3:
....: print (foo)
....: if foo != 4:
....: print ("checked too")
....:
3
checked too
status = True sets status to True already so just return status no need for an else