Jun-12-2020, 07:27 PM
Well it's good that you are creating a function and you started off OK except you should realize that the very first thing you do in the function is erase the list (lst) that you need to iterate over. You will require two lists for this task. One list you will loop over and read to get the words to process. Another list you will write to every time through the loop to store the length of each word. So you will iterate through the list of words you are given and each time through you will add a new item to the new list you created to store all the lengths of the words. When you are done the lists should be the same size.
Now please stop using range() and iterate over the items the way python is designed to. Look at this code snippet:
Now you want to create a new, empty list and fill it with the length of each word while you loop through it and you already know how to initialize it:
Now you just need to look in the python docs for that function (I already gave you a hint) and then convert the whole thing into a function like you were doing so that you return the list instead of printing it.
Now please stop using range() and iterate over the items the way python is designed to. Look at this code snippet:
word_list = ["one", "two", "three", "four"] for word in word_list: print word print len(word)It is much easier and less confusing to loop through the list by the items in it than to use range and have to index each item. This a one of the best things about python so take advantage!
Now you want to create a new, empty list and fill it with the length of each word while you loop through it and you already know how to initialize it:
word_list = ["one", "two", "three", "four"] length_list = [] # Creates a new empty list to fill with the length of each word. for word in word_list: # See, no using range() or len() needed. print word # And no need to index either. The variable 'word' updates automatically on each loop! print len(word) length_list.______(len(word)) # You need the function that adds an item to a list. print length_list # Print out your list of lengths.
Now you just need to look in the python docs for that function (I already gave you a hint) and then convert the whole thing into a function like you were doing so that you return the list instead of printing it.
"So, brave knights, if you do doubt your courage or your strength, come no further, for death awaits you all with nasty, big, pointy teeth!" - Tim the Enchanter