You are on page 1of 7

Discussion Forum Unit 5 – Strings and Iterations

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 lower

case 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 incor

rect results, 

and describe why the result is incorrect.

#1-------------------------------------------

def any_lowercase1(s):

    # Only the first letter in string is checked.

    # Returns True if condition is met otherwise false.

    for c in s:

        # c represents the first slice(letter) in s(string argument)

        if c.islower():

            # if this first letter is in lowercase return the boolean True

            return True

        else:

            return False
The function is incorrect since it only evaluates to True 

if the first letter is lowercase.

Example 1 shows it evaluates to True when the first letter is lowercase

Code

>>> any_lowercase1("pETER")

Output 

True

Example 2 shows it evaluates to False when the first letter is NOT lowercase

Code

>>> any_lowercase1("Peter")

Output                                 

False

#2--------------------------------------------

def any_lowercase2(s):

    # begins with checking the first letter of the string argument: same as above

    for c in s:

        # The string 'c' has nothing to do with the string argument so

        # as long as there is a c in s(as long as the argument string

        # contains something even if it is space),

        # it will evaluate to the string "True"

        if 'c'.islower():

            return 'True'
        else:

            return 'False'

'''

This function is incorrect since it ALWAYS evaluates to the string 'True' 

even when there is no letter, whatever the character, 

as long as there is a character (#$@!-* blank, etc.)

in between the " "(string quotes)

Examples

Code

>>> any_lowercase2("pETER")

>>> any_lowercase2("Peter")

>>> any_lowercase2(" ")

>>> any_lowercase2("-")

Output                                 

'True'

'True'

'True'

'True'

#3 ---------------------------------
def any_lowercase3(s):

    # Just as above it checks each letter in the string argument

    # if it is in lowercase it stores the boolean value in the variable <flag>

    # NB: The boolean value in <flag> changes as c moves through the string argument (s)

    # [from one letter to the next] until it gets to the last letter in the string

    # which becomes the last boolean value stored as <flag>

    # the "return flag" statement outputs that last boolean value

    for c in s:

        flag = c.islower()

        # print(flag)     #adding a print(flag) statement here helps

        # you see the different boolean values stored as c.islower() changes in s

    return flag

'''

The function is incorrect since it only evaluates to True 

when the last letter in the string is in lowercase 

Examples

Code

>>> any_lowercase3("pETER")

>>> any_lowercase3("Peter")

Output                                 

False

True
#4 ----------------------------------

def any_lowercase4(s):

    # This is basically an if/else conditional

    # where the original boolean stored in flag is False

    # so the condition is false for c in s (and it is stored as the value in flag)

    # unless c.islower() applies to c then the boolean True is stored as the value in flag.

    # When that occurs (as soon as flag evaluates to True),

    # flag = flag or c.islower() would evaluate to True

    # i.e. flag = True or True

    # hence all remaining values of c in s just evaluates to True

    flag = False

    for c in s:

        flag = flag or c.islower()

        # print(flag)         #Use the print statement here

         #to see what happens to flag everytime

         #c changes in s

     # the return flag statement outputs the last boolean value of flag

    return flag

This function is correct since once it identifies a letter in lowercase, 

every other check evaluates to True and the final return statement is True. 

Examples

Code
>>> any_lowercase4("pETER")

>>> any_lowercase4("Peter")

>>> any_lowercase4("PETEr")

Output                                 

True

True

True

#5 ----------------------------------

def any_lowercase5(s):

    # Here it checks if any letter in the string argument is NOT in lowercase

    # and returns false even if other letters may be in lowercase

    # however if (only if) ALL the letters in the string argument are lowercase then it returns True

    for c in s:

        if not c.islower():

            return False

    return True

'''

This function is not correct since it only evaluates to True 

when ALL the letters in the string arguments are in lowercase. 
Examples

Code

>>> any_lowercase4("pETER")

>>> any_lowercase4("Peter")

>>> any_lowercase4("PETEr")

>>> any_lowercase4("peter")

Output                                 

False

False

False

True

Reference:

Downey, A. (2015). Think Python: How to think like a computer scientist. Green Tea Press.

http://www.thinkpython2.com

You might also like