def trim(s): if s[:1]==' ': return trim(s[1:]) if s[-1:] == ' ': return trim(s[:-1]) return s ts = input("Have a try:") print(trim(ts))
这应该是最符合这章的答案了,不过最后两句可以直接删掉,没啥意义。。
简洁
还有递归函数,不错
Sign in to make a reply
霍墨