在Hangman游戏中发挥难度功能

时间:2019-03-27 06:37:20

标签: python python-3.x

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

3 个答案:

答案 0 :(得分:0)

一个可能的问题是,您试图读取目标文件两次,并且从未第一次关闭它。考虑只读取一次输入文件:

while True:
    difficulty = int(input("""choose your difficulty,
    1 = EASY
    2 = INTERMEDIATE
    3 = HARD
    4 = VERY HARD
    5 = INSANE"""))

    if difficulty >= 1 and difficult <= 5:
        break
    else:
        print("Please enter a difficulty level of 1 to 5")

word = random.choice(open("Level"(difficulty).txt).read().split())

此外,input()返回一个字符串,因此,如果要将difficulty视为整数,则将input()的结果强制转换。

答案 1 :(得分:0)

您正在尝试阅读两次单词。将if语句中的行替换为: TypeError: return arrays must be of ArrayType 不要忘记在if-elif块之前声明'word'。并在if语句的末尾删除该行。 word=(random.choice(open("Level1.txt").read().split())) 另外,将输入视为其他答案中建议的字符串。 这应该适合我

答案 2 :(得分:0)

在这里,您尝试将难度附加到“级别”字符串中。

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字符串中正确字母的首次出现(如果该字母出现多次,则可能会多次出现)。您需要获取每次出现正确字母的位置。