def longest(words): try: # attempt the for loop and see if it goes awry empty = True for w in words: # this might cause a TypeError if not isinstance(w, str): raise TypeError('elements must be strings') empty = False if empty: raise ValueError('sequence must be nonempty') # technically, we should have done the real work above as well, # as we may have just stripped all the data out of the iterable