我正在为单词益智游戏编写代码,但无法弄清楚如何将文本行包含到循环中

问题描述

我正在为单词益智游戏编写代码,并尝试在“ _ _ _ _ _”之前添加“到目前为止的答案是”行,如下面的输出图像中所示

Output image

import random
import re

显示说明

infile = open('wp_instructions.txt','r')
line = infile.readline()
while line:
    print(line.strip())                 #prints line
    line = infile.readline()            #loops the iteration
infile.close()    
words = ['apple','banana','watermelon','kiwi','pineapple','mango']
word = random.choice(words)

symbol = "_"                             
blanklines = len(word) * symbol
status = "The answer so far is "
print(status,end=" ") 

guesses = ''

这里可以使用任意匝数

turns = 4


while turns > 0:

计算用户失败的次数

Failed = 0

输入中的所有字符。 一个单词一次。

对于字符中的字符:

    # comparing that character with
    # the character in guesses
    if char in guesses: 
        print(char,end= ' ')

    else:
                  
        print("_",end= ' ')
        
        # for every failure 1 will be
        # incremented in failure
        Failed += 1


if Failed == 0:
    # user will win the game if failure is 0
    # and 'You Win' will be given as output
    print("Good job! You found the word",word + '!') 
    endgame_button = input('Press enter to end the game.')
    if endgame_button == '':        
        break

# if user has input the wrong alphabet then
# it will ask user to enter another alphabet
guess = input("\nGuess a letter (" + str(turns) + " " + "guesses remaining): ")

# every input character will be stored in guesses 
guesses += guess 

# check input with the character in word
if guess not in word:

    turns -= 1

    
    if turns == 0:
        print('Not quite,the correct word was ' + str(word)+ '. Better luck next time')
        endgame_button = input('Press enter to end the game.')
        if endgame_button == '':
                break

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)