python - 如何使用 while 或 for 循环替换列表中的部分字符串?
问题描述
我的代码是一个基本的刽子手游戏。我的问题是我的用户输入不断出现不稳定,我很确定这是因为我使用错误的循环......
我已经观看了数小时的视频并阅读了很多以前提出的问题......我试图只使用我所看到的所有建议,虽然有些建议像我当前的代码一样工作,但它最终会导致另一个问题。我真的是在乞求帮助。如果您查看我的代码,您会发现当我真正了解自己在做什么时,我会以极其详细的方式向自己解释一切。
#Word Puzzle Version 1
# Fill in the blank to guess the word.
import random, time
def main():
# display instructions
filename = 'instructions.txt'
mode = 'r'
file=open(filename,mode)
instructions=file.read()
print(instructions)
# display what is to be guessed
#import words file
filename2 = 'words.txt'
# Step 1 - Open the file
infile = open(filename2,mode)
# Step 2 - Read the file
content = infile.read()
# Step 3 - Strip the content of any leadning or trailing newline characters
content = content.strip()
# Step 4 - Additional clean up with splitlines function
all_words = content.splitlines()
# Randomly choose a word from list all_words
#I want only 1 word to be guessed
number_of_words = 1
#A sample of 1 word from my cleaned up list of words
word_list = random.sample(all_words,number_of_words)
# Use choice function from random module to choose sample from word list
answer = random.choice(word_list)
#Display word to be guessed
#Calculate the length of each word
Length = len(answer)
Underscores = (Length)*'_ '
#Prompt user to solve the puzzle
answer_so_far = 'The answer so far is '
#print('The answer so far is '+'_ '*(Length))
print(answer_so_far, Underscores)
#Prompt the player to fill in the blanks
Guess_a_letter = 'Guess a letter: '
guess =input(Guess_a_letter)
guess = guess.lower()
#If players guess in word replace the underscore
# Loop through the letters in the real word
for i in (answer):
if guess == i:
#print(answer_so_far, end = ' ')
print(guess, end = ' ')
elif guess != i:
print('_', end = ' ')
#Check if players guess is in the word
while guess:
if guess in answer:
print('\nGood job! You found the word '+(answer)+'.')
break
else:
print(answer_so_far,Underscores)
print('Not quite, the correct word was '+(answer)+'. Better luck next time.')
break
#End of game
#time.sleep(1.5)
input('Press enter to end the game.')
main ()
正确时,实际结果应该显示如下:
指示
到目前为止的答案是_ _ _ _ _
猜一个字母:a
到目前为止的答案是_ _ _ a _
好工作!你找到了(随机词)!
按回车结束游戏。
不正确时:
指示
到目前为止的答案是_ _ _ _ _
猜一个字母:a
到目前为止的答案是_ _ _ _ _
不完全的。正确的词是(随机词)。下次运气更好。
按回车结束游戏。
解决方案
你必须循环直到你所有的字母都被猜到为止。同时,您在第一个字母后中断循环。你的代码应该是这样的:
# read file, select random answer
guessed = False
# display number of letters in answer as underscores
while not guessed:
# get a letter from user input
# show placeholders with all guessed letters
if {some check - were all letters of answer guessed?}:
guessed = True
input("Press Enter to exit")
检查可能是这样的:创建一个集合,将用户尝试过的每个字母都添加到这个集合中,并检查是否set(answer) in your_set
推荐阅读
- jenkins - 参数被覆盖后如何将“当前构建参数”传递给其他项目?
- c++ - 无符号字符字符串
- javascript - 点击注册 1d Battleship 游戏。使用数组记录以前的用户输入,然后与当前输入交叉检查它们
- c# - 字符串未被识别为有效的日期时间
- angular - 验证限制 1 个月(Angular 4+)
- python - 以编程方式减去不同维度的栅格
- react-native - TypeError:无法读取未定义的“数据”属性
- post - 从 POST 请求传递数据并广播到 Micronaut 中的 websocket
- performance - “数据 URL”是提高网站图标加载速度的好方法吗?
- java - 为什么我会收到 NumberFormatException 错误?