當(dāng)我執(zhí)行下面的代碼時(shí)
if suggest in word: print("\nYes!",guess, "is in the word!") # 創(chuàng)建一個(gè)新變量 (so_far) 來(lái)包含猜測(cè) new = "" i = 0 for i in range(len(word) )): if guess == word[i]: new +=guess else: new += so_far[i] so_far = new
我正在返回這個(gè)錯(cuò)誤
string index out of range python
解決方案
python 字符串索引超出范圍的最佳解決方案
在您看來(lái),您確定了 so_far = new much。你可以嘗試下面的代碼:文章來(lái)源:http://www.zghlxwxcb.cn/article/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錯(cuò)誤。文章來(lái)源地址http://www.zghlxwxcb.cn/article/446.html
到此這篇關(guān)于TypeError:string index out of range python的文章就介紹到這了,更多相關(guān)內(nèi)容可以在右上角搜索或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!