简体   繁体   English

Python子手游戏循环

[英]Python hangman game for loop

When I guess the letter it keeps applying the letter. 当我猜到这封信时,它会不断应用这封信。 for example say the word is words, then I would guess the letter d, then it would do ddddd. 例如,说单词是单词,那么我猜字母d,然后它就等于ddddd。 it uses that letter for the whole word. 它使用整个字母的字母。 here is my code. 这是我的代码。

import random
print(" Welcome to the HangMan game!!\n","You will have six guesses to get the answer correct, or you will loose!!!",)

lines = open("../WordsForGames.txt").read() 
line = lines[0:] 
                    #lines 24-28 Randomly generate a word from a text file
words = line.split() 
myword = random.choice(words)
print(myword)


words = myword
fake = '_'*len(myword)
count = 0
print(fake)
guesses = 0
guess = input("Enter a letter you would like to guess: ")
fake = list(fake)  #This will convert fake to a list, so that we can access and change it.

for re in range(0, len(myword)):#For statement to loop over the answer (not really over the answer, but the numerical index of the answer)
    fake[re] = guess    #change the fake to represent that, EACH TIME IT OCCURS
    print(''.join(fake))

if guess != (''.join(fake)):
    print("The letter ", guess,"was in the word. Guess another letter please!")
    guess = input("Enter another letter you would like to guess: ")      
    fake[re] = guess    #change the fake to represent that, EACH TIME IT OCCURS
    print(''.join(fake))

This part is the culprit: 这是罪魁祸首:

for re in range(0, len(myword)):#For statement to loop over the answer (not really over the answer, but the numerical index of the answer)
    fake[re] = guess    #change the fake to represent that, EACH TIME IT OCCURS
    print(''.join(fake))

This code replace every element in fake with guess 这段代码用guess替换了fake每个元素

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM