在 Hangman 游戏中制作难度功能

我是 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("""

________                   

|/   |                         

|   (_)                      

|   /|\                             

|    |                          

|   /                            

|                                  

|___                              

""")


阿晨1998
浏览 232回答 3
3回答

翻翻过去那场雪

在这里,您尝试将难度附加到“级别”字符串。word = (random.choice(open("Level"(difficulty).txt).read().split()))你可以试试这样的word = (random.choice(open("Level{0}.txt".format(difficulty)).read().split()))实际获得您期望的文件名。有了这个,您还可以删除if difficulty == X:部分,但您需要注意检查您尝试使用用户输入打开的文件是否确实存在。而且您还需要查看您的 checkLetter 方法,因为该行guess_word[word.index(c)] = c只会改变 guess_word 字符串中正确字母的第一次出现(如果字母出现不止一次,可能会改变多次)。您需要获取正确字母每次出现的位置。

呼啦一阵风

您正试图将一个单词读两遍。将 if 语句中的行替换为: word=(random.choice(open("Level1.txt").read().split())) 不要忘记在 if-elif 块之前声明 'word'。并删除 if 语句末尾的行。 word = (random.choice(open("Level"(difficulty).txt).read().split()))此外,按照其他答案中的建议将输入视为字符串。这应该根据我的工作

慕虎7371278

一个可能的问题是您尝试读取目标文件两次,并且第一次从未关闭它。考虑只读取一次输入文件:while True:&nbsp; &nbsp; difficulty = int(input("""choose your difficulty,&nbsp; &nbsp; 1 = EASY&nbsp; &nbsp; 2 = INTERMEDIATE&nbsp; &nbsp; 3 = HARD&nbsp; &nbsp; 4 = VERY HARD&nbsp; &nbsp; 5 = INSANE"""))&nbsp; &nbsp; if difficulty >= 1 and difficult <= 5:&nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; print("Please enter a difficulty level of 1 to 5")word = random.choice(open("Level"(difficulty).txt).read().split())此外,input()返回一个字符串,因此如果您想将difficulty视为整数,则将结果从input().
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python