What's new

Python Discussion Assignment

paulghette1026

Honorary Poster
Established
Joined
Aug 27, 2017
Posts
433
Reaction
110
Points
213

Discussion Assignment​

Each of the following Python functions is supposedto 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


The code and its output must be explained technically whenever asked. The explanation can be provided before or after the code, or in the form of code comments within the code. For any descriptive type question, Your answer must be at least 150 words.
End your discussion post with one question related to programming fundamentals learned in this unit from which your colleagues can formulate a response or generate further discussion. Remember to post your initial response as early as possible, preferably by Sunday evening, to allow time for you and your classmates to have a discussion.
 
Function 1 (any_lowercase1):
What it actually does:
This function checks if the first character of the string is lowercase. If it is, it returns True; otherwise, it returns False. It does not continue checking the rest of the characters in the string.

Example argument that produces incorrect results:
any_lowercase1("Hello")
This argument produces incorrect results because the function only checks the first character, 'H', which is uppercase. It immediately returns False without checking the remaining characters.

Function 2 (any_lowercase2):
What it actually does:
This function always returns the string 'True'. It does not check the characters of the string argument at all. Instead, it checks if the string 'c' is lowercase (which it is), and returns the string 'True' regardless of the input.

Example argument that produces incorrect results:
any_lowercase2("Hello")
This argument produces incorrect results because the function does not check the characters of the input string. It always returns the string 'True', regardless of the input.

Function 3 (any_lowercase3):
What it actually does:
This function checks if the last character of the string is lowercase. It assigns the result of c.islower() to the variable flag within the for loop. After the loop completes, it returns the value of flag, which represents whether the last character is lowercase.

Example argument that produces incorrect results:
any_lowercase3("HELLO")
This argument produces incorrect results because the function only checks the last character, 'O', which is uppercase. It assigns False to flag and returns False.

Function 4 (any_lowercase4):
What it actually does:
This function checks if any character in the string is lowercase. It initializes the flag variable to False and then iterates through each character in the string. If any character is lowercase, it updates flag to True. Finally, it returns the value of flag.

Example argument that produces incorrect results:
any_lowercase4("123")
This argument produces incorrect results because the function does not handle non-alphabetic characters. It checks each character and assigns False to flag, and since no character is lowercase, it returns False.

Function 5 (any_lowercase5):
What it actually does:
This function checks if all characters in the string are lowercase. It iterates through each character in the string and if any character is not lowercase, it immediately returns False. If it completes the loop without returning False, it returns True.

Example argument that produces incorrect results:
any_lowercase5("Hello World")
This argument produces incorrect results because the function checks each character individually. It returns False when it encounters the space character ' ' and stops checking the remaining characters. However, there are lowercase letters in the string, so the function should return True.

Question: What are the potential drawbacks of using multiple return statements in a function?
 

Similar threads

Back
Top