我正在学习 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)
(这样做是从字典中随机选择一个单词作为秘密单词,让玩家对其进行猜测,每尝试 5 次,玩家获得 1 个小费,即单词的一个字母,尝试 16 次后,您输了并且必须重新启动)。
我不知道为什么会随机发生这种情况,并且一直试图找出一个小时但没有成功。感谢您提前提供帮助。
猛跑小猪
相关分类