当我执行下面的代码时
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模板网!