NameError:未定义名称'pigLatin'

时间:2018-05-25 07:35:51

标签: python

我的代码是:

def vowelIndex(wd) :

    def getInitialCs(wd) :
        return wd[:vowelIndex(wd)]

    def getTheRest(wd) :
        return wd[vowelIndex(wd):]

    def pigLatin(wd) :
         if len(wd) == 0: return ''
         elif wd[0] in 'aeiou' :
              return wd + 'way'
         else :
              return getTheRest(wd) + getInitialCs(wd) + 'ay'

当我输入PigLatin('yellow')时出现此错误:

    NameError                                 Traceback (most recent call last)
    <ipython-input-128-e025299840a1> in <module>()
    ----> 1 pigLatin('yellow')

NameError: name 'pigLatin' is not defined

我想要这个结果:

pigLatin('yellow')

'ellowyay'

我不知道出了什么问题,请帮帮我

1 个答案:

答案 0 :(得分:1)

您需要正确定义vowelIndex,而不是将其他函数放入其中(修复缩进)。

def vowelIndex(wd):
    return 1          #<-- need a proper algorithm

def getInitialCs(wd):
    return wd[:vowelIndex(wd)]


def getTheRest(wd):
    return wd[vowelIndex(wd):]


def pigLatin(wd):
    if len(wd) == 0:
        return ''
    elif wd[0] in 'aeiou':
        return wd + 'way'
    else:
        return getTheRest(wd) + getInitialCs(wd) + 'ay'

print(pigLatin('yellow'))

输出:

ellowyay