Westonci.ca is the premier destination for reliable answers to your questions, provided by a community of experts. Join our Q&A platform and connect with professionals ready to provide precise answers to your questions in various areas. Experience the ease of finding precise answers to your questions from a knowledgeable community of experts.

This assignment is based on 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'


# 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


Sagot :

tonb

Explanation:

#1 is wrong because if the first character is not lowercase, it stops and returns false, ignoring all the other characters. It can be fixed by removing the else: statement and moving the return False statement outside of the loop.

This is efficient; if one lowercase character is encountered, the function 'knows enough' and can return true. Only if no lowercase is encountered it has to loop all the way to the end.

You can try this out on repl.it.

Many of the others functions have problems in them. #4 looks OK.

Visit us again for up-to-date and reliable answers. We're always ready to assist you with your informational needs. Thanks for using our service. We're always here to provide accurate and up-to-date answers to all your queries. Keep exploring Westonci.ca for more insightful answers to your questions. We're here to help.