错误"类型'类型的对象'没有len()"

时间:2015-07-19 03:11:54

标签: python typeerror

    struct stack(
    int[10] data;
    int top;

    void int();     // wrong here
    ....
)

这是我写过的代码。它应该输出:

vowlist=['a','e','i','o','u']
def piglatin(s):
    if len(s)==1:
        if s[0] in vowlist:
            return s[0]+'way'
        else:
            return s[0]+'ay'
    elif s[0]==' '*len(s):
        return ' '
    elif len(s)>1:
        if s[0] in vowlist or (s[0]=='y' and s[1] not in vowlist):
            return s[0:]+'way'
        else:
            return new(s)
def new(s):
    global str
    if s[0] not in vowlist:
        str=s[0]+new(s[1:])
    else:
        return s[len(str):]+str[0:]+'ay'
print piglatin('school')
print piglatin('yttribium')
print piglatin('yolo') 

但它会给出错误oolschay yttribiumway oloyay 为什么会这样?

1 个答案:

答案 0 :(得分:3)

str是Python中的一种类型。为变量使用不同的标识符。改变这个方法:

def new(s):
    global str
    if s[0] not in vowlist:
        str=s[0]+new(s[1:])
    else:
        return s[len(str):]+str[0:]+'ay'

到此:

def new(s):
    global my_str
    if s[0] not in vowlist:
        my_str=s[0]+new(s[1:])
    else:
        return s[len(my_str):]+my_str[0:]+'ay'