下面我用islower()编写了5个不同的函数。

目标是检查给定函数是否满足目的,即检查给定字符串是否至少包含一个小写。

我还附上了原因,请检查我的分析/解释是否有效。在#Example1: True

def any_lowercase1(s):
for c in s:
if c.islower():
return True
else:
return False
#Example2: False
## this function checks only the string 'c' is lower, which always returns True
def any_lowercase2(s):
for c in s:
if 'c'.islower():
return 'True'
else:
return 'False'
#Example3: False
##the result only depends on the last letter of given string
def any_lowercase3(s):
for c in s:
flag = c.islower()
return flag
#Example4: False
##Similar to Example3, this function also depends on the last character of given string
def any_lowercase4(s):
flag = False
for c in s:
flag = flag or c.islower()
return c.islower()
#Example5: False
## This function returns False if it conatains more than one Captial Letter.
def any_lowercase5(s):
for c in s:
if not c.islower():
return False
return True
print(any_lowercase4('RrR'))
#above must be true, but it returns False