Python TypeError:序列项0:预期的str实例,发现NoneType [英] Python TypeError: sequence item 0: expected str instance, NoneType found

查看:147
本文介绍了Python TypeError:序列项0:预期的str实例,发现NoneType的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从一本书中复制了以下代码:

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()))

我收到此错误:

Traceback (most recent call last):
  File "<pyshell#4>", line 1, in <module>
    cambiaPronome('ciao')
  File "C:\Python33\2.py", line 6, in cambiaPronome
    return ''.join(map(getWord, list(frase)))
TypeError: sequence item 0: expected str instance, NoneType found

什么是

推荐答案

getWord 函数未明确返回任何内容,因此它隐式返回 None 。尝试返回一些东西,例如:

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)

这篇关于Python TypeError:序列项0:预期的str实例,发现NoneType的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆