0

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!

Carmen Moreno
  • 115
  • 2
  • 8
  • 1
    An immediate issue seems to be you are removing items while iterating over a list. This is a well reported to result in unintended consequences [for instance](https://stackoverflow.com/questions/6022764/python-removing-list-element-while-iterating-over-list/6024599). Why not just `new_word=word_list[index]` rather than using pop? – DarrylG Apr 28 '20 at 10:49
  • 1
    Don't iterate and alterate a list at the same time. See dupe. – Patrick Artner Apr 28 '20 at 10:49
  • You don't need to remove the words from the list with `pop`. Just leave them there and all will be fine. – Thierry Lathuille Apr 28 '20 at 10:50
  • In particular, check out [this answer to a another question](https://stackoverflow.com/a/6260097/5014455) to understand why removing from a list while iterating over it will result in unexpected behavior. – juanpa.arrivillaga Apr 28 '20 at 11:00

1 Answers1

0

You must not iterate and modify a list at the same time, try this instead:

poem = input("Enter your poem here: ")
word_list = poem.split(" ")
print(word_list)
length_list = len(word_list)
new_word_list = []

for index in range(length_list):
    if len(word_list[index]) <= 3:
        new_word_list.append(word_list[index].lower())
    elif len(word_list[index]) >= 7:
        new_word_list.append(word_list[index].upper())
    else:
        new_word_list.append(word_list[index])

new_word_list = " ".join(new_word_list)
print(new_word_list)
RMPR
  • 3,368
  • 4
  • 19
  • 31