Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

THIS IS PYTHON: Define a function called all_extreme() that takes a tuple of str

ID: 639704 • Letter: T

Question

THIS IS PYTHON: Define a function called all_extreme() that takes a tuple of strings and returns True if all of the strings contain exclamation points. If any of the strings are missing exclamation points, then it should return False. This function must use the function extreme() that you defined in the previous problem. So all_extreme(("!","Bogus!","!YES!")) and all_extreme(("Nifty!","!!")) should both return True, but all_extreme(("Cool!","Okay...")) and all_extreme(("square","...")) should both return False.

My code so far is:

def all_extreme(text, text1, text2):
    """takes a tuple of strings and returns True if all of the strings contain exclamation points and returns false otherwise

    String -> Boolean"""
    for letter in (text, text1, text2):
        if letter == "!":
            return True
    return False

My for-loop is only working for the first variable but does not seem to work for others. Also, how can I expand my argument so that if someone entered n-strings as arguments ("---", "---", "---" ....) the function will still work?

Explanation / Answer

In the provided code in the question it is able to return the final string value.

According to the code, you need to check all the strings should that contain “!” mark if so the function should return a value “True” or else it should return “False”.

But in the code it is able to return the final string result.

To check all the strings that are passed are containing the “!” mark one has to maintain the Boolean array.

When each string is checked, the respective boolean value is append into the array.

After appending the Boolean values, check whether all the string contains a Boolean value “True”.

If it contains return True, else return False.

Here is the modified code is as shown below:

Program code:

def all_extreme(text, text1, text2):

    flag = []

    actualflag = False

    i=0

    for letter in (text, text1, text2):       

        if "!" in letter:

            flag.append(True)

        else:

            flag.append(False)

        i=i+1

   

    if flag[0]==True and flag[1]==True and flag[2]==True:

        actualflag = True

    else:

        actualflag = False       

    return actualflag

   

print(all_extreme("","Bogus!","!YES!"))

-------------------------------------------------------------------------------

Sample output:

When the function is passed as all_extreme("","Bogus!","!YES!")

sh-4.2# python3 main.py                                 

False   

When the function is passed as all_extreme("!","Bogus!","!YES!")

sh-4.2# python3 main.py                                 

True