Python Typeerror: Sequence Item 0: Expected Str Instance, Nonetype Found
I copied this code from a book: lyst = {'Hi':'Hello'} def changeWord(sentence): def getWord(word): lyst.get(word, word) return ''.join(map(getWord, sentence.split()
Solution 1:
The getWord
function doesn't explicitly return anything, so it implicitly returns None
. Try returning something, e.g.:
def getWord(word):
return lyst.get(word, word)
Solution 2:
The problem is you're trying to write to a string a type which is NoneType. That's not allowed.
If you're interested in getting the None
values as well one of the things you can do is convert them to strings.
And you can do it with a list comprehension, like:
return ''.join([str(x) for x in map(getWord, sentence.split())])
But to do it properly in this case, you have to return something on the inner function, else you have the equivalent to return None
Post a Comment for "Python Typeerror: Sequence Item 0: Expected Str Instance, Nonetype Found"