索引超出范围,同时循环进入循环python

发布于 2025-02-06 20:45:39 字数 391 浏览 4 评论 0原文

我不明白为什么我有一个问题的追溯(最近的电话): 文件“ main.py”,第7行,in 如果每个==单词[i]: indexError:字符串索引超出范围。

感谢您的帮助。

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)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

与风相奔跑 2025-02-13 20:45:39

这里有几个答案,但没有人提到他为什么遇到错误。

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']

循环时不需要使用,您可以得到结果
使用一个条件进行循环。

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


print(text_final)

# output
SomEwaCkyRunESaTouRiSTfound

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
浅忆 2025-02-13 20:45:39

您只需要检查文本中的字符是否以文字存在。如果存在,则将该字符的上限附加到text_final,否则仅附加相同的字符。

写出解决问题的众多解决方案之一

>>> 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'

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'
破晓 2025-02-13 20:45:39

您的I变量在满足您的wir循环条件之前已达到值6,并且由于您的“单词”变量没有足够[5]是您的最后一个字母,用单词变量。

如果您打印它,这就是您的OP的外观。

['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

无法说出如何解决这个问题,直到您对要做什么更清楚地提出问题为止。

希望这会有所帮助。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文