共计 1000 个字符,预计需要花费 3 分钟才能阅读完成。
当我执行下面的代码时
if suggest in word:
print("nYes!",guess, "is in the word!")
# 创建一个新变量 (so_far) 来包含猜测
new = ""
i = 0
for i in range(len(word) )):
if guess == word[i]:
new +=guess
else:
new += so_far[i]
so_far = new
我正在返回这个错误
string index out of range python
解决方案
python 字符串索引超出范围的最佳解决方案
在您看来,您确定了 so_far = new much。你可以尝试下面的代码: 文章来源:https://www.toymoban.com/diary/python/446.html
if guess in word:
print("nYes!", guess, "is in the word!")
# Create a new variable (so_far) to contain the guess
new = ""
i = 0
for i in range(len(word)):
if guess == word[i]:
new += guess
else:
new += so_far[i]
so_far = new # unindented this
之后,您将解决字符串索引超出范围的 Python 错误。 文章来源地址 https://www.toymoban.com/diary/python/446.html
到此这篇关于 TypeError:string index out of range python 的文章就介绍到这了, 更多相关内容可以在右上角搜索或继续浏览下面的相关文章,希望大家以后多多支持 TOY 模板网!
原文地址:https://www.toymoban.com/diary/python/446.html
如若转载,请注明出处:如若内容造成侵权 / 违法违规 / 事实不符,请联系站长进行投诉反馈,一经查实,立即删除!
正文完