I am trying to do the following:
- get string input, input like a poem, verse or saying split the string into a list of individual words determine the length of the list Loop
- the length of the list by index number and for each list index:
if a word is short (3 letters or less) make the word in the list lowercase
if a word is long (7 letters or more) make the word in the list uppercase
My code is the following:
poem=input("Enter your poem here: ")
word_list=poem.split()
print(word_list)
length_list=len(word_list)
new_word=""
for index in range(0,length_list):
print(len(word_list[index]))
if len(word_list[index])<=3:
new_word=word_list.pop(index)
word_list.append(new_word.lower())
elif len(word_list[index])>=7:
print(word_list[index])
new_word=word_list.pop(index)
word_list.append(new_word.upper())
print(word_list)
When I run it I get the following:
Enter your poem here: Little fly, Thy summer’s play My thoughtless hand Has brushed away. Am not I A fly like thee? Or art not thou A man like me? # user input
['Little', 'fly,', 'Thy', 'summer’s', 'play', 'My', 'thoughtless', 'hand', 'Has', 'brushed', 'away.', 'Am', 'not', 'I', 'A', 'fly', 'like', 'thee?', 'Or', 'art', 'not', 'thou', 'A', 'man', 'like', 'me?']
6 #length first item on the list (Little)
4 #length second item on the list (fly)
3 #length third item on the list (Thy)
4 #length fifth item on the list (Play). Why is not calculating the index of the fourth item ("summer's) ??
2
4
3
5
2
1
3
5
2
3
1
4
3
2
2
3
3
3
2
3
2
2
['Little', 'fly,', 'summer’s', 'play', 'thoughtless', 'hand', 'brushed', 'away.', 'not', 'A', 'like', 'thee?', 'art', 'thou', 'man', 'like', 'thy', 'has', 'i', 'or', 'a', 'my', 'fly', 'me?', 'not', 'am']
If was wondering what is wrong in my code so that the loop doesn't show the lengths of the items in the list that are longer than 7 and what can I do to solve this issue? Thanks!