刽子手游戏:词语末尾有不受欢迎的角色

时间:2015-12-15 08:54:30

标签: python

这是我的刽子手游戏代码的一部分。它用于所有四个困难,但当它用于我的“疯狂”难度(使用单词文件中的单词)时,它会在单词的末尾添加一个额外的符号,这意味着你无法赢得游戏。它为.txt文件中的每个单词执行此操作。 在python窗口中使用数组时,此代码可以正常工作。

def insane():
    global score  
    print ("This words may contain an apostrophe. \nStart guessing...")

    time.sleep(0.5)

    word = random.choice(words).lower()
    print (word)
    guesses = ''
    fails = 0
    while fails >= 0 and fails < 10:  #try to fix this         
        failed = 0                
        for char in word:      
            if char in guesses:    
                print (char,)    

            else:
                print ("_"),     
                failed += 1    
        if failed == 0:        
            print ("\nYou won, WELL DONE!")
            score = score + 1
            print ("your score is,", score)
            difficultyINSANE()

        guess = input("\nGuess a letter:").lower()
        guesses += guess   
        if guess not in word:  
            fails += 1        
            print ("\nWrong")

            if fails == 1:
                print ("You have", + fails, "fail....WATCH OUT!" )
            elif fails >= 2 and fails < 10:
                print ("You have", + fails, "fails....WATCH OUT!" ) 
            if fails == 10:           
                print ("You Loose\n")
                print ("your score is, ", score)
                print ("the word was,", word)
                score = 0 
                difficultyINSANE()

修改

这就是我读的单词

INSANEWORDS = open("create.txt","r+") 
words = [] 
for item in INSANEWORDS:
   words.append(item) 

2 个答案:

答案 0 :(得分:1)

每个单词的末尾都有一个\n。 在添加之前,您应该删除\n的单词:

INSANEWORDS = open("create.txt", "r+")
words = []
for item in INSANEWORDS:
    words.append(item.strip('\n'))

<强>之前:

enter image description here

<强>后:

enter image description here

答案 1 :(得分:0)

如果您从文本文件中读取一行时我的猜测是正确的,您还可以在单​​词末尾阅读新行字符 \ n ,您可以使用以下字符删除:

    new winston.transports.File({
        filename: logLocation,
        handleExceptions: true,
        json: true,
        maxsize: 100000,
        maxFiles: 5,
        colorize: false,
        tailable: true
    }),