简体   繁体   English

Python. function 中的反向字符串

[英]Python. reverse string in a function

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

###does anyone know what I may be doing wrong ###有谁知道我可能做错了什么

The problem with your code is that you have + somestring[0] inside the rev method invocation.您的代码的问题是您在 rev 方法调用中有+ somestring[0] In other words change that line to...换句话说,将该行更改为...

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM