所以我有这两个脚本,它们的内容相同,但是循环内的if语句不同:
根据我的理解,if, if
测试这两个条件,而if, elif
测试直到第一个条件True
并忽略下一个条件。
为什么我的教练告诉我if, elif
是正确的答案?都不都是有效答案吗?
def has_letter_cases(s: str) -> bool:
"""Return True if and only if s contains at least one lowercase letter
and an uppercase letter
>>>has_letter_cases('abcABC')
True
>>>has_letter_cases('abc123')
False
>>>has_letter_cases('ABCXYZ')
False
"""
upper = False
lower = False
for ch in s:
if ch.islower():
lower = True
if ch.isupper():
upper = True
return bool(upper and lower)
def has_letter_cases(s: str) -> bool:
"""Return True if and only if s contains at least one lowercase letter
and an uppercase letter
>>>has_letter_cases('abcABC')
True
>>>has_letter_cases('abc123')
False
>>>has_letter_cases('ABCXYZ')
False
"""
upper = False
lower = False
for ch in s:
if ch.islower():
lower = True
elif ch.isupper():
upper = True
return bool(upper and lower)