For each function, describe what it actually does when called with a string argument. If it does not correctly check for lowercase letters, give an example argument that produces incorrect results, and describe why the result is incorrect.
# 1
def any_lowercase1(s):
for c in s:
if c.islower():
return True
else:
return False
# 2
def any_lowercase2(s):
for c in s:
if 'c'.islower():
return 'True'
else:
return 'False'
# 3
def any_lowercase3(s):
for c in s:
flag = c.islower()
return flag
# 4
def any_lowercase4(s):
flag = False
for c in s:
flag = flag or c.islower()
return flag
# 5
def any_lowercase5(s):
for c in s:
if not c.islower():
return False
return True
# 1
def any_lowercase1(s):
for c in s: # Checking the first character and
if c.islower(): # immediately displaying the True if
return True # lower and False if upper
else:
return False
# Returns true only if the first letter in a string is lower.
# 2
def any_lowercase2(s):
for c in s:
if 'c'.islower(): # Always checking string 'c',
return 'True' # so always True
else:
return 'False'
# Always returns true.
# 3
def any_lowercase3(s):
for c in s: # Draws output at the last character
flag = c.islower()
return flag
# Returns true only in the last character is lower.
# 4
def any_lowercase4(s):
flag = False
for c in s: # Changes and leaves the flag on the
flag = flag or c.islower() # True in case you have met at least
return flag # once lowercase
# Correctly checks for lowercase letters in any position.
# 5
def any_lowercase5(s):
for c in s:
if not c.islower(): # Will return False if it finds at
return False # least one uppercase
return True
# Returnes true only if all symbols in the string are lower.
Comments
Leave a comment