我是 python 新手,需要我编写的一段代码的帮助。截至目前,我的程序功能齐全,但是我无法让难度功能按预期工作。我有多个 .txt 文档,其中的单词难度不同。我希望用户能够选择他们将玩的难度。目前,当我尝试运行该程序时,我收到一条错误消息,提示“未定义单词”。任何帮助是极大的赞赏
import random
print ("WELCOME, YOU ARE PLAYING HANGMAN!")
input("Press Enter to continue...")
def printhangmen(guesses):
if (guesses == 0):
print("""
_________
|/
|
|
|
|
|
|___
""")
if (guesses == 1):
print("""
_________
|/ |
|
|
|
|
|
|___
""")
elif (guesses == 2):
print("""
_________
|/ |
| (_)
|
|
|
|
|___
""")
elif (guesses == 3):
print("""
________
|/ |
| (_)
| |
| |
|
|
|___
""")
elif (guesses == 4):
print("""
_________
|/ |
| (_)
| /|
| |
|
|
|___
""")
elif (guesses == 5):
print("""
_________
|/ |
| (_)
| /|\
| |
|
|
|___
""")
elif (guesses == 6):
print("""
________
|/ |
| (_)
| /|\
| |
| /
|
|___
""")
elif (guesses == 7):
print("""
________
|/ |
| (_)
| /|\
| |
| / \
|
|___
"""" You guessed wrong. The correct word was: " + str(word))
guesses = 8
difficulty = input("""choose your difficulty,
1 = EASY
2 = INTERMEDIATE
3 = HARD
4 = VERY HARD
5 = INSANE""")
print("you have chosen", difficulty)
print("Please begin guessing")
if difficulty == 1:
(random.choice(open("Level1.txt").read().split()))
elif difficulty == 2:
(random.choice(open("Level2.txt").read().split()))
elif difficulty == 3:
(random.choice(open("Level3.txt").read().split()))
elif difficulty == 4:
(random.choice(open("Level4.txt").read().split()))
elif difficulty == 5:
(random.choice(open("Level5.txt").read().split()))
word = (random.choice(open("Level"(difficulty).txt).read().split()))
guess_word = ['_' for x in word]
def checkLetter(letter, word, guess_word):
for c in word:
if c == letter:
guess_word[word.index(c)] = c
while '_' in guess_word and guesses > 0:
print(guess_word)
guess = input('Letter: ')
if guess in word:
print("correct letter")
else:
print("incorrect")
guesses -= 1
print (guesses," guesses left")
printhangmen(8-guesses)
checkLetter(guess, word, guess_word)
else:
print("congrats, you won. If you would like to proceed to the next level, please press enter")
input()