Home > other >  index out of range, while loop into for loop python
index out of range, while loop into for loop python

Time:06-13

I don't understand why i have an issue Traceback (most recent call last): File "main.py", line 7, in if each == words[i]: IndexError: string index out of range.

Thanks for your help.

text_final = []
text = "somewackyrunesatouristfound"
words = "secret"
i = 0
while i < len(words):
   for each in text:
     if each == words[i]:
       text_final.append(each.upper())
       i =1
     else:
       text_final.append(each)
    
print(text_final)

CodePudding user response:

Couple of answers here but no body mentions why he got the error.

text_final = []
text = "somewackyrunesatouristfound"
words = "secret"
i = 0
while i < len(words):
    for each in text:
     if each == words[i]:
       text_final.append(each.upper())
       # i  = 1
       # you should not increment i here
     else:
       text_final.append(each)

    # instead increment i here
    i  = 1

print(text_final)

# but by using above code you won't get result you want, you can avoid 
index exceeded error
# expected output
['S', 'o', 'm', 'e', 'w', 'a', 'c', 'k', 'y', 'r', 'u', 'n', 'e', 'S', 'a', 't', 'o', 'u', 'r', 'i', 'S', 't', 'f', 'o', 'u', 'n', 'd', 's', 'o', 'm', 'E', 'w', 'a', 'c', 'k', 'y', 'r', 'u', 'n', 'E', 's', 'a', 't', 'o', 'u', 'r', 'i', 's', 't', 'f', 'o', 'u', 'n', 'd', 's', 'o', 'm', 'e', 'w', 'a', 'C', 'k', 'y', 'r', 'u', 'n', 'e', 's', 'a', 't', 'o', 'u', 'r', 'i', 's', 't', 'f', 'o', 'u', 'n', 'd', 's', 'o', 'm', 'e', 'w', 'a', 'c', 'k', 'y', 'R', 'u', 'n', 'e', 's', 'a', 't', 'o', 'u', 'R', 'i', 's', 't', 'f', 'o', 'u', 'n', 'd', 's', 'o', 'm', 'E', 'w', 'a', 'c', 'k', 'y', 'r', 'u', 'n', 'E', 's', 'a', 't', 'o', 'u', 'r', 'i', 's', 't', 'f', 'o', 'u', 'n', 'd', 's', 'o', 'm', 'e', 'w', 'a', 'c', 'k', 'y', 'r', 'u', 'n', 'e', 's', 'a', 'T', 'o', 'u', 'r', 'i', 's', 'T', 'f', 'o', 'u', 'n', 'd']

no need to use while loop for your case, you can get result using one for loop itself with condition.

text_final = ""
for character in text:
    if character in words:
        text_final  = character.upper()
    else:
        text_final  = character.lower()


print(text_final)

# output
SomEwaCkyRunESaTouRiSTfound

CodePudding user response:

you just need to check if character in text is present in words or not. if it is present then append upper case of that character to text_final, otherwise append that same character only.

writing one of many solution way to solve your problem

>>> text_final = []
>>> text = "somewackyrunesatouristfound"
>>> words = "secret"
>>> i, j = 0, 0
>>> while i < len(words):
...     while j<len(text):
...             if words[i]==text[j]:
...                     text_final.append(text[j].upper())
...                     j =1
...                     break
...             else:
...                     text_final.append(text[j])
...             j =1
...     i =1
... 
>>> text_final
['S', 'o', 'm', 'E', 'w', 'a', 'C', 'k', 'y', 'R', 'u', 'n', 'E', 's', 'a', 'T']
>>> ''.join(text_final)
'SomEwaCkyRunEsaT'

CodePudding user response:

Your i variables is reaching the value 6 before your while loop condition is getting satisfied, and since your "words" variables does not have enough letters within it, when it reaches words[6], it will give a out of range error as words[5] is your last letter in the words variables.

This is how your op will look if you print it.

['S'] 1
['S', 'o', 'm', 'E'] 2
['S', 'o', 'm', 'E', 'w', 'a', 'C'] 3
['S', 'o', 'm', 'E', 'w', 'a', 'C', 'k', 'y', 'R'] 4
['S', 'o', 'm', 'E', 'w', 'a', 'C', 'k', 'y', 'R', 'u', 'n', 'E'] 5
['S', 'o', 'm', 'E', 'w', 'a', 'C', 'k', 'y', 'R', 'u', 'n', 'E', 's', 'a', 'T'] 6

Cant say on how you can solve this, until you make the question clearer on what you want to do.

Hope this helps.

  • Related