python在未完成循环时遇到的问题
我有一个刽子手游戏,它是完整的,但在我赢了或输了之后,它会终止程序。我正试图在其中加入一个(虽然没有完成)声明,但似乎无法使其工作。非常感谢您的帮助!下面是代码,这是第一部分:python在未完成循环时遇到的问题,python,while-loop,Python,While Loop,我有一个刽子手游戏,它是完整的,但在我赢了或输了之后,它会终止程序。我正试图在其中加入一个(虽然没有完成)声明,但似乎无法使其工作。非常感谢您的帮助!下面是代码,这是第一部分: import drawHangman import turtle import random def main(): #Do not change or remove code from herel window = turtle.Screen() window.setup(400, 400,
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()
我不会给你一个完整的解决方案,这样你就可以自己解决了,但是你缺少的基本原则是,你需要修改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
。我不会给您一个完整的解决方案,这样您就可以自己解决问题,但是您缺少的基本原则是,您需要在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
。我不会给您一个完整的解决方案,这样您就可以自己解决问题,但是您缺少的基本原则是,您需要在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
。我不会给您一个完整的解决方案,这样您就可以自己解决问题,但是您缺少的基本原则是,您需要在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
。如alksdjg所说,您对done
变量的更改需要在while循环中才能生效,如果您希望播放器继续运行,您需要考虑为什么要使用break
语句
另一件事,考虑你的第79行和第80行;什么是x
以及何时检查输入(“您想再次播放吗?”)
的用途
如果您重新考虑所有这些,游戏的重放功能应该能够正常工作。就像alksdjg所说的,您对
done
变量的更改需要在while循环中才能生效,如果你想让玩家继续玩,你需要考虑为什么你会有break
语句
另一件事,考虑你的第79行和第80行;什么是x
以及何时检查输入(“您想再次播放吗?”)
的用途
如果您重新考虑所有这些,游戏的重放功能应该能够正常工作。就像alksdjg所说的,您对
done
变量的更改需要在while循环中才能生效,如果你想让玩家继续玩,你需要考虑为什么你会有break
语句
另一件事,考虑你的第79行和第80行;什么是x