如何解决我正在为单词益智游戏编写代码,但无法弄清楚如何将文本行包含到循环中
我正在为单词益智游戏编写代码,并尝试在“ _ _ _ _ _”之前添加“到目前为止的答案是”行,如下面的输出图像中所示
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 举报,一经查实,本站将立刻删除。