簡體   English   中英

Python子手游戲循環

[英]Python hangman game for loop

當我猜到這封信時,它會不斷應用這封信。 例如,說單詞是單詞,那么我猜字母d,然后它就等於ddddd。 它使用整個字母的字母。 這是我的代碼。

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))

這是罪魁禍首:

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))

這段代碼用guess替換了fake每個元素

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM