为什么我的python代码中的append()操作计算为None?

时间:2015-09-03 19:34:51

标签: python

所以我正在写一个猪拉丁语翻译程序,当我试图将每个单词的第一个字母附加到单词的末尾时,我遇到了麻烦。我调试了代码,我发现行“list = listEnd.append(firstLetter)”(下面的粗体)正在评估为None,这导致下一行“list = list.append('ay')”到崩溃。谁能告诉我为什么会这样?

错误信息是:Traceback(最近一次调用最后一次):   文件“C:\ Python34 \ pigLatinTranslator.py”,第33行,in     列表= listEnd.append(firstLetter).append( 'AY') AttributeError:'NoneType'对象没有属性'append'

谢谢!

def getSentence():
    print('Enter a sentence you would like to translate to pig Latin(letters and           spaces only)')
    sentence=input()
    return sentence

def isStartsWithConsonant(word):
    for i in 'bcdfghjklmnpqrstvwxyz':
        if word.startswith(i):
            return True

while True:
    sentence=getSentence()
    #if isValidSentence(sentence):
    sentenceList=sentence.split()
    listLists=[]
    for w in sentenceList[:]:
        wList=[]
        for l in w:
            wList.append(l)
        listLists.append(wList)

    for list in listLists:
        if isStartsWithConsonant(list[0]):
            listEnd=list[1:]
            firstLetter=list[0]
            list=listEnd.append(firstLetter).append('ay')

        else:
            listEnd=list[1:]
            firstLetter=list[0]
            list=listEnd.append(firstLetter)
            list.append('way')
    translatedSentence=''
    for list in listLists:
        for l in list:
            translatedSentence.append(l)
        translatedSentence.append(' ')
    print('Your sentence translated to pig Latin is:'+translatedSentence)
    print('Do you want to translate another sentence?')
    if not input().startswith(y):
        break

1 个答案:

答案 0 :(得分:1)

append在python中返回None。你可以这样做:

listEnd.append(firstLetter)
list = listEnd

list = listEnd + [firstLetter]