【发布时间】:2019-09-16 17:49:49
【问题描述】:
我正在做一个 codewars 挑战,你必须在字符串上找到最小的线,我决定使用 len() 函数,但每当运行代码时,它都会给我以下错误:
Traceback (most recent call last):
File "main.py", line 4, in <module>
test.assert_equals(find_short("bitcoin take over the world maybe who knows perhaps"), 3)
File "/home/codewarrior/solution.py", line 5, in find_short
if word.len() < l:
AttributeError: 'str' object has no attribute 'len'
这是有缺陷的代码,我真的找不到它有什么问题:
def find_short(s):
foo = s.split()
l = 100
for word in foo:
if word.len() < l:
l = word.len()
else:
continue
return l # l: shortest word length
【问题讨论】:
-
AttributeError: 'str' object has no attribute 'len'- 此消息中有什么不清楚的地方? -
使用
len(word) -
在提出问题之前,请确保自己进行一些研究。搜索您提供的错误消息会导致这个已回答的问题:'str' object has no attribute 'len'。
标签: python python-3.x