Jun-12-2020, 04:02 PM
First thing is that your loop will work but in python there is a better way to loop over list (and other iterable sequences). Instead of forcing an index like you did:
So back to your problem. Can you see how easy it would be to print the length of each word? If you can print those words and you know how to use len()…
Then, you will want to create a new list with all the lengths in it. So you will need to start with an empty list:
Inside the loop you will need to add the length of the word to the end of your my_length_list by appending (hint!) it.
See how far you get and let us know if you get stuck...
for x in range(len(a)): print a[x]It's more pythonic to simply loop over the items themselves:
for x in a: print aI should mention now that 'a' is a really bad variable name and you should get in the habit of using meaningful names. Even "my_list" would do. Also, do you have to use python 2? It's not being supported anymore so unless it's required by your class you should upgrade to python 3.
So back to your problem. Can you see how easy it would be to print the length of each word? If you can print those words and you know how to use len()…
Then, you will want to create a new list with all the lengths in it. So you will need to start with an empty list:
my_length_list = [] # Empty brackets create an empty list.Put that before your loop to initialize your my_length_list each time the program runs.
Inside the loop you will need to add the length of the word to the end of your my_length_list by appending (hint!) it.
See how far you get and let us know if you get stuck...
"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