Answer to Question #246584 in Python for Raymond Ekwubiri

Question #246584
Exercise 8.4 from your textbook. 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.

# 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


# 5

def any_lowercase5(s):
for c in s:
if not c.islower():
return False
return True
1
Expert's answer
2021-10-04T23:10:39-0400
def any_lowercase1(s):
    for c in s:
        if c.islower():
            return True
        else:
            return False
#Using the for loop we check through the argument if it is lower case or not
#The function returns true if they are all lowercase and false if any of the letters in s is upper case

def any_lowercase2(s):
    for c in s:
        if 'c'.islower():
            return  'True'
        else:
            return 'False'
any_lowercase2('Wright')


#The code isn't correct, first we change 'c' to c, 'True' to True and 'False' to False as in the code below

'True'

def any_lowercase2(s):
    for c in s:
        if c.islower():
            return  True
        else:
            return False
any_lowercase2('Wright')

False

def any_lowercase5(s):
    for c in s:
        if not c.islower():
            return False
        return True
any_lowercase2('Wright')


#we pass the string as argument s, we then check if c is in lowercase, we return true, else return False

False

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