Answer to Question #189928 in Python for FORTUNE AIDOO

Question #189928

Each of the following Python functions is supposed to check whether its argument has any lowercase letters.


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.


# 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
Expert's answer
2021-05-06T07:33:47-0400

Function any_lowercase3, when called with a string argument in a for loop, iterates over each character in the string and checks if that character is lowercase and writes the value to the flag variable. On output, the function returns flag only for the last character in the string. The function will return True only if the last character in the string is lowercase. In any other case, the function will return False. Therefore, the function will not work correctly for the string 'abcdE'.


Function any_lowercase4 when called with a string argument, creates a flag variable with the value False. The for loop iterates over each character of the string and, using the boolean expression 'flag or c.islower ()', if at least one character in the string is lowercase, sets the flag variable to True. On exit, the function returns the value of the flag variable. The function works correctly.


Function any_lowercase5 when called with a string argument in a for loop, iterates over each character in the string and, using a ternary if statement, returns False if the character is not a string argument. If all characters in the string are lowercase, then the function returns True. The function does not work correctly if the string contains at least one non-lowercase character. For example, the function will return False for the string 'abCde'


Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS