【问题标题】:python while not done loop problemspython while not done 循环问题
【发布时间】:2015-09-29 02:47:11
【问题描述】:

我有这个刽子手游戏,一切都完成了,但是在我赢或输后它会终止程序。我正在尝试在其中加入一个(虽然没有完成)声明,但似乎无法让它发挥作用。非常感谢一些帮助!这是下面的代码,这是第一部分:

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)

【问题讨论】:

标签: python while-loop


【解决方案1】:

我不会给你一个完整的解决方案,所以你可以自己解决,但你缺少的基本原则是你需要在 while 循环本身中修改你的 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 的行 - 看起来您想在这些点周围退出程序,因此您可能还想在此时重新分配 done .

【讨论】:

    【解决方案2】:

    就像 alksdjg 所说,您对 done 变量的更改需要在 while 循环中才能使其产生任何效果,并且您需要考虑为什么要使用 break 语句玩家继续前进。

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-23
      • 1970-01-01
      • 2022-12-07
      • 2020-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-19
      相关资源
      最近更新 更多