python虽然没有完成循环问题

时间:2015-09-29 02:47:12

标签: python while-loop

我有这个Hangman游戏,它已经完成了,但在我获胜或松散之后终止了该程序。我试图在其中加入一个(虽然没有完成)声明,但似乎无法让它工作。一些帮助非常感谢!以下是代码,这是第一部分:

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

这是代码的第二部分,它描绘了头部,身体,手臂,腿等所有东西:

def default(babbage):
    #Start drawing stand
    babbage.penup()
    babbage.setpos(0,-50)
    babbage.pendown()
    babbage.back(100)
    babbage.fd(50)
    babbage.left(90)
    babbage.forward(175)
    babbage.right(90)
    babbage.forward(50)
    babbage.right(90)
    babbage.forward(25)
    babbage.right(90)
    #End drawing stand

def drawHead(babbage):
    babbage.pencolor("red")
    babbage.circle(15)
    babbage.penup()
    babbage.left(90)
    babbage.forward(30)
    babbage.pendown()

def drawBody(babbage):
    babbage.forward(65)
    babbage.back(40)
    babbage.right(90)

def drawRightArm(babbage):
    babbage.forward(30)
    babbage.right(180)
    babbage.forward(30)

def drawLeftArm(babbage):
    babbage.forward(30)
    babbage.back(30)


def drawRightLeg(babbage):
    #Move to lower body
    babbage.right(90)
    babbage.forward(40)
    #Draws the leg
    babbage.right(45)
    babbage.forward(40)
    babbage.right(180)
    babbage.forward(40)
    babbage.right(90)

def drawLeftLeg(babbage):
    babbage.forward(40)

def reset(babbage):
    babbage.reset()
    default(babbage)

2 个答案:

答案 0 :(得分:2)

我不会给你一个完整的解决方案,所以你可以自己解决,但你缺少的基本原则是你需要在while循环中修改你的布尔值本身

您想要的结构是:

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

这样,每次我们通过while循环时,done都有机会成为True。一旦它发生,我们就可以退出循环。

您拥有的结构是:

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

if语句是在while循环之外,这意味着我们可以进入done = True的唯一方法是退出循环。但是,除非done已经True,否则我们无法退出循环,那么我们怎样才能进入该重新分配线?问题是我们没有机会在循环中更改done的值。

我建议您查看循环内部的行break - 看起来您想要围绕这些点退出程序,因此您可能需要重新分配{{1那时也是。

答案 1 :(得分:1)

就像alksdjg所说的那样,你改变done变量需要在while循环中才能使它产生任何影响,你需要考虑为什么你有break语句你希望玩家继续前进。

另一件事,考虑你的第79和80行;什么是x以及您何时检查input("Would you like to play again?")用于什么?

如果您重新考虑所有这些,游戏的重放功能应该可以正常运行。