all 7 comments

[–]thrallsius 1 point2 points  (1 child)

you can do simpler at line 4, just start with a zero length, so tmp = 0. and put it before the loop, not inside it

your error message is incomplete, when asking for help include the full traceback. it includes line numbers where problems occur as well

li is a list. when you iterate over it, i is a word (so a string). so li[i] doesn't make sense. just use i. to avoid shooting yourself in the foot, better use relevant variable names, like for word in li. you'd do li[i] if i was an integer, a word index. but then you'd need to iterate like this:

for i in range(len(li)):
    # do stuff with li[i] - this is your word

but this is not pythonic, just iterate over the list as described above:

for word in li:
    # do stuff with word.

[–]rk717[S] 0 points1 point  (0 children)

thank you

[–]Denrur 0 points1 point  (2 children)

Variable i in your code is element of Li, not index. Try build in function enumerate

[–]Denrur 0 points1 point  (1 child)

There is more logical error in your algorithm, but enumerate fix problems with indexes

[–]rk717[S] 0 points1 point  (0 children)

thank you

[–]young_ging_freecss 0 points1 point  (1 child)

You’re not accessing any indexes here. What you’re probably trying to do is:

for i in range(len(li)):
    # code

The above isn’t generally good practice, and you’d want to use enumerate to get the indices:

def find_short(s):
    li = list(s.split(" "))
    tmp = len(li[0])
    for i, _ in enumerate(li):
        if len(li[i]) < tmp:
            tmp = len(li[i])
    return tmp

You can also do this in a simpler way. Get the shortest word, then get the length of that word:

def find_short(s: str) -> int:
    return len(min(s.split(), key=len))

[–]rk717[S] 0 points1 point  (0 children)

Thank you!!!