【发布时间】:2014-07-07 09:35:19
【问题描述】:
both_ends给定一个字符串
s,返回一个由前2和后2组成的字符串 原始字符串的字符,所以'banana'给出'bana'但是, 如果字符串长度小于 2,则返回空字符串。
我的代码:
def both_ends(string):
for item in string:
if len(item) < 2:
return ["empty"]
else:
(item [0])(item [1]).append(item (len(item)-1))(item (len(item) -2))
string=["jelly"]
both_ended=both_ends(string)
print both_ended
错误信息:
File "both_ends.py", line 18, in <module>
both_ended=both_ends(string)
File "both_ends.py", line 14, in both_ends
(item [0])(item [1]).append(item (len(item)-1))(item (len(item) -2))
TypeError: 'str' object is not callable
【问题讨论】:
-
你用什么来学习Python?您对
()的使用是......不是Python。 -
return ["empty"]返回一个包含一个元素的列表(字符串“empty”)。我认为您应该只返回一个没有内容的字符串 (=>"")。
标签: python string append character