首页 > 解决方案 > 如何使用 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

到目前为止的答案是_ _ _ _ _

不完全的。正确的词是(随机词)。下次运气更好。

按回车结束游戏。

编辑:为了澄清“古怪” 输出错误

输出错误

标签: pythonpython-3.xlistloops

解决方案


你必须循环直到你所有的字母都被猜到为止。同时,您在第一个字母后中断循环。你的代码应该是这样的:

# 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


推荐阅读