definitely getting closer
def longest_word(text):
words = text.split()
longest_word = max(words, key=len)
print(longest_word)
but I get the correction:
I called your function as:
longest_word('Monty Python and the Holy Grail')
:::text
Python
Your function returned None
, but is printing the expected string.
Your function should should return the value instead of printing it.
So of course I go:
def longest_word(text):
words = text.split()
longest_word = max(words, key=len)
return(longest_word)
print(longest_word)
but now i’m met with:
Python exception
Traceback (most recent call last):
File "HOME:/solution.py", line 3, in longest_word
longest_word = max(words, key=len)
ValueError: max() arg is an empty sequence
A ValueError
indicates that a function or an operation received an argument of the right type, but an inappropriate value.
I do not recognize this error message. I am guessing that the problem is with the function max
. Its docstring is:
‘’'max(iterable, [, default=obj, key=func]) → value max(arg1, arg2, args, *[, key=func]) → value
With a single iterable argument, return its biggest item. The default keyword-only argument specifies an object to return if the provided iterable is empty. With two or more arguments, return the largest argument.‘’’
Exception raised on line 3 of file HOME:/solution.py.
1| def longest_word(text):
2| words = text.split()
-->3| longest_word = max(words, key=len)
^^^^^^^^^^^^^^^^^^^
4| return(longest_word)
words: []
len: <builtin function len>
max: <builtin function max>
I am stumped to be honest