简体   繁体   English

为什么我得到 IndexError: list assignment index out of range

[英]why do i get the IndexError: list assignment index out of range

I am trying to get know the postion of a letter in a list and how many time it comes up.我想知道一封信在列表中的位置以及它出现了多少次。 when i get those positions i wanted to replace same postitions with in the list right_user_input .当我得到这些职位时,我想用列表right_user_input替换相同的职位。

but whenever i try to do that it returns this error IndexError: list assignment index out of range.但是每当我尝试这样做时,它都会返回此错误IndexError: list assignment index out of range.

can anyone tell why this is?谁能告诉这是为什么?

import random
import re
from words import words

# the word that the user needs to guess
the_guess_word = random.choice(words)

n = 0
# puts the random picked word in a list
l_guess = list(the_guess_word)
box = l_guess

# somethings are not yet finished
print("Welcome To The Guessing Game . \n You get 6 Guesses . The Words Are In Dutch But There Is 1 English Word . "
  "\n If You Would Want To Try Again You Can Press (ctrl+Z)"
  f"\n \n Your Word Has {len(the_guess_word)} letters ")

class hangman:
    t = len(box)
    right_user_input = []

    # should create the amount of letters in the word
    right_user_input.append(t * ".")


    while True:
        # the user guesses the letter
        user_guess = input("guess the word : ")

        # if user guesses it wrong 6 times he or she loses
        if n >= 6 :
            print("you lose!")
            print(f'\n the word was {the_guess_word}')
            break

            # loops over until user gets it right or loses
        if user_guess not in the_guess_word:
                print("\n wrong guess try again ")
                n += 1

        # when user gets it right the list with the dots gets replaced by the guessed word of the user
        if user_guess in the_guess_word :
            print("you got it right")

            # finds the position of the guessed word in the to be guessed the word
            for m in re.finditer(user_guess, the_guess_word):
                right_user_input[m.end()] = user_guess

                print(right_user_input)

# this the error that i get
# i tried searching around but usually people have empty strings in my case that is not the issue .
# and the value in the right_user_input is len(the_guess_word) * "."

   Traceback (most recent call last):
  File "C:/Users/Admin/PycharmProjects/hangman/main.py", line 16, in <module>
    class hangman:
  File "C:/Users/Admin/PycharmProjects/hangman/main.py", line 38, in hangman
    right_user_input[m.end()] = user_guess
IndexError: list assignment index out of range

The problem is that you didn't setup right_user_input correctly.问题是您没有正确设置right_user_input right_user_input.append(t * ".") just makes the variable equal a single string of t dots ( ["....."] ) instead of t strings of 1 dot ( [".", ".", ".", ".", "."] ). right_user_input.append(t * ".")只是使变量等于 t 个点的单个字符串 ( ["....."] ) 而不是 1 个点的 t 个字符串 ( [".", ".", ".", ".", "."] )。

To fix this, declare the variable this way:要解决此问题,请以这种方式声明变量:

right_user_input = ["." for i in range(len(the_guess_word))] right_user_input = ["." for i in range(len(the_guess_word))] . right_user_input = ["." for i in range(len(the_guess_word))]

Also, replace right_user_input[m.end()] = user_guess with此外,将right_user_input[m.end()] = user_guess

right_user_input[m.end()-1] = user_guess since arrays start at 0. right_user_input[m.end()-1] = user_guess ,因为 arrays 从 0 开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我得到一个 IndexError: list index out of range - Why do I get an IndexError: list index out of range 为什么会出现“ IndexError:列表索引超出范围”错误? - Why do I get a “IndexError: list index out of range” error? 为什么我得到 IndexError: list index out of range - Why do I get IndexError: list index out of range 为什么我会得到 IndexError: string index out of range? - Why do I get IndexError: string index out of range? 为什么我得到“IndexError:字符串索引超出范围”? - Why do I get "IndexError: string index out of range"? 为什么我会得到:IndexError:元组索引超出范围? - why do i get: IndexError: tuple index out of range? 为什么会出现“ IndexError:列表索引超出范围”? (美丽汤) - Why do I get a “IndexError: list index out of range”? (Beautiful Soup) 为什么在我的 Python 代码 Cows and Bulls 中出现“IndexError: list index out of range”? - Why do I get "IndexError: list index out of range" in my Python code Cows and Bulls? 为什么在解析字典时出现 IndexError: list index out of range? - Why do i get IndexError: list index out of range while parsing dictionary? 将图像转换为CSV文件时,为什么会出现IndexError:列表索引超出范围? - Why do I get IndexError: List index out of range when turning images into CSV file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM