Python。函数中的反转字符串

时间:2021-02-18 02:55:31

标签: python

def rev(somestring):


    if somestring is None:
        return somestring
    if (len(somestring)) <=1:
        return somestring

    return rev(somestring[1:]+somestring[0])

somestring = "house"
print(rev(somestring))

###有谁知道我做错了什么

1 个答案:

答案 0 :(得分:1)

您的代码的问题在于您在 rev 方法调用中有 + somestring[0]。 换句话说,将该行更改为...

return rev(somestring[1:])+somestring[0]
相关问题