holaprofesor
holaprofesor

Reputation: 271

AttributeError with for loops

I want to 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 needs to use the function extreme() that I have already defined. 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. Here is what I have so far:

def extreme(x):
    """returns True if the string contains at least one exclamation point

    str -> str"""
    if x.find('!') != -1:
        return True
    else:
        return False

def all_extreme(x):
    """returns true if all of the strings contain exclamation points

    str, str... -> str"""
    for word in x:
        if extreme(x) == True:
            return True
        else:
            return False

The problem that I am having is that I getting a AttributeError: 'tuple' object has no attribute 'find'. I am open to any and all suggestions.

Upvotes: 0

Views: 48

Answers (1)

TigerhawkT3
TigerhawkT3

Reputation: 49320

It's extreme(word), not extreme(x).

Also, you will encounter a problem with the return statements - they'll return after only one iteration. Save a True flag at the beginning of all_extreme(), then if one of the words in x is not extreme, set the flag to False. Then return the value of the flag.

This task can also be accomplished more simply:

def all_extreme(x):
    return all('!' in word for word in x)

Upvotes: 2

Related Questions