Python-Tkinter:当存在索引时,表示我的索引超出范围时出错

Python-Tkinter:当存在索引时,表示我的索引超出范围时出错,python,list,python-3.x,error-handling,tkinter,Python,List,Python 3.x,Error Handling,Tkinter,所以,我正在为一个学校项目制作这个刽子手游戏,遇到了一个问题,我一辈子都搞不清楚为什么会发生这样的事情 word_list = ["APPLE", "PEAR", "BANNANA"] word = word_list [random.randint(0,2)] hidden_word = ["_ " * len(word)] print (word) 这段代码是一个列表,然后是一个字符串变量: word = word_list [random.randint(0,2)] 然后,通过获取

所以,我正在为一个学校项目制作这个刽子手游戏,遇到了一个问题,我一辈子都搞不清楚为什么会发生这样的事情

word_list = ["APPLE", "PEAR", "BANNANA"]

word = word_list [random.randint(0,2)]

hidden_word = ["_ " * len(word)]
print (word)
这段代码是一个列表,然后是一个字符串变量:

word = word_list [random.randint(0,2)]
然后,通过获取长度,我创建了一个新的列表,该列表是隐藏的单词,使用“u”来隐藏:

hidden_word = ["_ " * len(word)]
然后我打印单词(用于开发)

关于有问题的代码

def click_1 (key):
    if str(key) in word:
        key_1 = word.index(key)
        print (key_1)
        hidden_word[key_1] = key
        print (hidden_word)
    else:
        print ("Nope")
    return letter

r = c = 0
for letter in string.ascii_uppercase:
    Button(letter_frame, text=letter, command=functools.partial(click_1, letter)).grid(row=r, column=c, sticky=W)
    c += 1
    if c > 12:
        c = 0
        r += 1
这使得Buton出现,当我点击带有字母的按钮时,它会检查它是否在word中,然后(此时)打印:

如果这个词是班纳纳。问题是当我按下A时:

1
出现,如果我按下其他按钮,则会出现以下错误:

Exception in Tkinter callback
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/idlelib/run.py", line 121, in main
    seq, request = rpc.request_queue.get(block=True, timeout=0.05)
  File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/queue.py", line 175, in get
    raise Empty
queue.Empty

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/tkinter/__init__.py", line 1475, in __call__
    return self.func(*args)
  File "/Users/alexeacott/Desktop/Hangman.py", line 24, in click_1
    hidden_word[key_1] = key
IndexError: list assignment index out of range
最后一行是最中间的,因为很明显,N超出了范围。我的问题是,为什么会发生这种情况,以及我能做些什么来修复


节日快乐

下一行用一个元素组成一个列表(元素是一个字长为“u…”的字符串):

访问元素(索引>0)会导致
索引器
,因为列表中只有一个元素

您可能需要创建多个元素的列表:

hidden_word = ["_ "] * len(word)


所以我需要做的是改变方括号?天哪,我是哑巴哈哈,谢谢!我还有一个额外的问题:现在我有了这个功能,当我点击按钮时,输出是正确的,但是如果有多个字母实例,它不会替换那个,因此如果一个字母重复,我就永远无法完成一个单词!我不知道从哪里开始:/@18166,
list.index
返回第一次出现的索引。不要使用它,而是迭代列表项,检查该项是否与输入字符匹配,并对匹配项执行适当的操作。
Exception in Tkinter callback
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/idlelib/run.py", line 121, in main
    seq, request = rpc.request_queue.get(block=True, timeout=0.05)
  File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/queue.py", line 175, in get
    raise Empty
queue.Empty

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/tkinter/__init__.py", line 1475, in __call__
    return self.func(*args)
  File "/Users/alexeacott/Desktop/Hangman.py", line 24, in click_1
    hidden_word[key_1] = key
IndexError: list assignment index out of range
hidden_word = ["_ " * len(word)]
hidden_word = ["_ "] * len(word)
>>> ["_ " * 3]
['_ _ _ ']
>>> ["_ "] * 3
['_ ', '_ ', '_ ']