python未完成时的循环问题

2024-06-07 01:30:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我有这个刽子手游戏,它都是完整的,但当我赢了或失去它终止程序。我试图在其中加入一个(虽然还没有完成)语句,但似乎无法实现工作。一些非常感谢您的帮助!下面是代码,这是第一部分:

import drawHangman
import turtle
import random

def main():

    #Do not change or remove code from herel
    window = turtle.Screen()
    window.setup(400, 400, 200, 200)
    HG = turtle.Turtle()
    drawHangman.default(HG)


    print(" Welcome to the HangMan game!!")
    print(" You will have six guesses to get the answer correct.")
    print(" If you reach the limit of six wrong guess, it will be GAME OVER!")
    print(" Below you will see how many letter are in the word,\n","for eveyone you get right the line will be replaced with the letter.")

    lines = open("../WordsForGames.txt").read() 
    line = lines[0:] #lines 21-24 Randomly generate a word from a text file
    words = line.split() 
    myword = random.choice(words)
    #print(myword)# this print the random word
    done = False
    words = myword
    guessed = '_'*len(myword)
    guessedLetters = ""
    guesses = 0
    correctGuess = 0
    print(guessed)#this prints the blank lines.

    while(not done):    
        while correctGuess != len(myword):
            guess = input("Enter a letter you would like to guess: ")
            guessed = list(guessed)  #This will convert fake to a list, so that we can access and change it.
            if len(guess) == 1 and guess.isalpha():
                if guessedLetters.find(guess) != -1:
                    print("(",guess,")letter has already been picked")
                else:
                    guessedLetters = guessedLetters + guess
                    index1 = myword.find(guess)
                    if index1 == -1:
                        print("The letter(", guess,")is not a correct letter in the word", ''.join(guessed))
                        guesses = guesses + 1
                        print("You have guessed(", guesses,")times")
                        print("Remember that you only get 6 guesses!")
                        if guesses == 1:
                            drawHangman.drawHead(HG)
                        elif guesses == 2:
                            drawHangman.drawBody(HG)
                        elif guesses == 3:
                            drawHangman.drawRightArm(HG)
                        elif guesses == 4:
                            drawHangman.drawLeftArm(HG)
                        elif guesses == 5:
                            drawHangman.drawRightLeg(HG)           
                        elif guesses == 6:
                            drawHangman.drawLeftLeg(HG)
                            print("You reached your limit of 6 guesses, GAME OVER! The word was ("+ myword + ").")
                            break
                    else:
                        correctGuess = correctGuess + myword.count(guess)
                        print("The letter(",guess,")is in the word")
                        for ch in range(0, len(myword)):#For statement to loop over the answer (not really over the answer, but the numerical index of the answer)
                            if guess == myword[ch]:
                                guessed[ch] = guess #change the fake to represent that, EACH TIME IT OCCURS
                                print(''.join(guessed))
                                print("The letter(",guess ,")was in the word. Great job keep going")
                                if correctGuess != len(myword):
                                    print("You have guessed wrong(", guesses,")times!.")
                                    print("Remember that you only get 6 guesses!")
                                elif guesses <= 0:
                                    print("You reached your limit of 6 guesses, GAME OVER! The word was ("+ myword + ").")
                                    break
            else:"Guess any letter you want!"
        if correctGuess == len(myword):
            print("Congratulations! You won!")

    if (x == "n"):
        input("Would you like to play again?")
        done = True
    else:
        drawHangman.reset(HG)        
main()

这是代码的第二部分,它绘制了头、身、臂、腿等所有元素:

^{pr2}$

Tags: thetoyoulenifhgwordprint
2条回答

我不打算给你一个完整的解决方案,这样你就可以自己解决了,但是你缺少的基本原则是你需要在while循环中修改while布尔值。在

您需要的结构是:

done = False
while (not done):
    *stuff*
    if [some condition meaning we should stop the loop]:
        done = True

这样,每次我们执行while循环时,done就有机会变成{}。一旦完成,我们就可以退出循环。在

你的结构是:

^{pr2}$

if语句位于while循环之外,这意味着我们可以到达done = True的唯一方法是退出循环。但是,除非done已经是{},否则我们无法脱离循环,那么我们如何才能到达重新分配行呢?问题是我们没有机会在循环中更改done的值。在

我建议您查看循环中放置break的行,看起来您希望围绕这些点退出程序,因此您可能还需要在该点重新分配done。在

就像alksdjg所说的,你对done变量的更改必须在while循环中才能起作用,如果你想让播放器继续运行,你需要考虑为什么要有break语句。在

另一件事,考虑第79行和第80行;什么是x,什么时候检查input("Would you like to play again?")是用来做什么的?在

如果你重新考虑所有这些,你的游戏回放功能应该可以正常工作。在

相关问题 更多 >