if和elif问题:哪个有效?

时间:2018-10-16 19:07:48

标签: python python-3.x

所以我有这两个脚本,它们的内容相同,但是循环内的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)

0 个答案:

没有答案