Python程序有时会以“退出”;索引器:字符串索引超出范围“;有时还不错

Python程序有时会以“退出”;索引器:字符串索引超出范围“;有时还不错,python,index-error,Python,Index Error,我正在学习Python,并决定升级一个版本的hangman游戏,该游戏在我观看的一个课程中用作示例,代码如下: import random word_dictionary = { 1: "mouse", 2: "house", 3: "show", 4: "see", 5: "leave", 6: "shower",

我正在学习Python,并决定升级一个版本的hangman游戏,该游戏在我观看的一个课程中用作示例,代码如下:

import random
word_dictionary = {
    1:  "mouse",
    2:  "house",
    3:  "show",
    4:  "see",
    5:  "leave",
    6:  "shower",
    7:  "showcase",
    8: "coding",
    9:  "elephant",
    10:  "apartment",
}

random_number = random.randint(1, 10)
random_word = word_dictionary[random_number]

secret_word = random_word
word_len = len(secret_word)
guess = None
tip = 0
tip_num = None
tip_previous = None
tip_position = None
tip_model_formula = "_"*word_len
tip_model_list = list(tip_model_formula)
send_tip = None

print("Word has", word_len, " letters")
while guess != secret_word:
    guess = input("Enter your guess: ")
    if guess != secret_word:
        print("Wrong answer! Try again!")
        tip += 1
        if tip == 5:
            tip_num = random.randint(0, word_len)
            tip_position = secret_word[tip_num]
            tip_model_list[tip_num] = tip_position
            send_tip = "".join(tip_model_list)
            tip_previous = tip_num
            print("here's a tip:\n" + send_tip)
        if tip == 10:
            tip_num = random.randint(0, word_len)
            while tip_num == tip_previous:
                tip_num = random.randint(0, word_len)
            tip_position = secret_word[tip_num]
            tip_model_list[tip_num] = tip_position
            send_tip = "".join(tip_model_list)
            tip_previous = tip_num
            print("here's a tip:\n" + send_tip)
        if tip == 15:
            tip_num = random.randint(0, word_len)
            while tip_num == tip_previous:
                tip_num = random.randint(0, word_len)
            tip_position = secret_word[tip_num]
            tip_model_list[tip_num] = tip_position
            send_tip = "".join(tip_model_list)
            tip_previous = tip_num
            print("here's a tip:\n" + send_tip)
        if tip == 16:
            print("You lost!")
            exit()

print("You win!")
(这样做的目的是从字典中随机选取一个单词作为秘密单词,让玩家猜一猜,每5次尝试,玩家就得到1个提示,即单词的一个字母,尝试16次后,你输了,必须重新开始)

当我运行它时,有时它会按预期运行,但有时它会退出并出现以下错误:

Traceback (most recent call last):
  File "E:\Program Files\PyCharm Community Edition 2020.2\plugins\python-ce\helpers\pydev\pydevd.py", line 1448, in _exec
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "E:\Program Files\PyCharm Community Edition 2020.2\plugins\python-ce\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "C:/Users/rafae/PycharmProjects/Beginning/Beg.py", line 55, in <module>
    tip_position = secret_word[tip_num]
IndexError: string index out of range
回溯(最近一次呼叫最后一次):
文件“E:\Program Files\PyCharm Community Edition 2020.2\plugins\python ce\helpers\pydev\pydevd.py”,第1448行,在_exec中
pydev_imports.execfile(文件、全局、局部)#执行脚本
文件“E:\Program Files\PyCharm Community Edition 2020.2\plugins\python ce\helpers\pydev\\u pydev\u imps\\u pydev\u execfile.py”,第18行,在execfile中
exec(编译(内容+“\n”,文件,'exec'),全局,loc)
文件“C:/Users/rafae/PycharmProjects/beging/Beg.py”,第55行,在
tip\u position=秘密单词[tip\u num]
索引器错误:字符串索引超出范围
我不知道为什么这种情况会随机发生,我已经试着弄清楚了一个小时没有成功。 提前感谢您的帮助。

您的错误在这里:

            tip_num = random.randint(0, word_len)
            tip_position = secret_word[tip_num]

randint
返回其第一个参数和第二个参数之间的数字,包括这两个参数。这意味着它可以返回
word\u len
的结果,该结果超过
secret\u word

TL的末尾;DR
random.randint(1,10)
有时选择10请提供预期值。显示中间结果与您预期的不同之处。您针对一个5行问题发布了50多行代码。插入适当的
print
语句以跟踪程序值和执行情况。然后你有一个很好的堆栈溢出问题。。。虽然您可能已经自己解决了问题,但到那时为止。:-)所以
tip\u position=random。建议选择(秘密词)
instead@Jean-弗朗索瓦·法布,那你怎么得到小费呢?它也用于其他事情…最简单的修复方法是只使用
random.randint(0,word\u len-1)
,然后其他任何东西都不需要更改。我认为使用
choice
的方法类似于
random.choice(I for I,enumerate中的字母(tip_model_list),如果字母='''.''''.''''.'这显然不是问题所在,因为即使在添加了安全检查之后,也不允许数字高于
secret_word
,问题仍然存在。您在多个位置有相同的错误。确保你在所有的地方都修复了它(或者更好的是,把它变成一个函数,这样你就不必在多个地方重复相同的逻辑)!