Python Hangman项目同时出现循环故障
在这个科罗纳病毒春假期间,我正在为班级做一个刽子手项目,但它不起作用。游戏()中的while循环不起作用。它一直完美地运行到最后,然后它不会退出while循环并运行win()。它停留在while循环中,说“猜一猜…”,不管我做了什么尝试都拒绝退出。有人能帮我弄清楚吗Python Hangman项目同时出现循环故障,python,Python,在这个科罗纳病毒春假期间,我正在为班级做一个刽子手项目,但它不起作用。游戏()中的while循环不起作用。它一直完美地运行到最后,然后它不会退出while循环并运行win()。它停留在while循环中,说“猜一猜…”,不管我做了什么尝试都拒绝退出。有人能帮我弄清楚吗 from datetime import time from time import sleep import sys import random Hangman1 = """ ____ | |
from datetime import time
from time import sleep
import sys
import random
Hangman1 = """
____
| |
| 0
|
|
|
_|_
| |______
| |
|__________|"""
Hangman2 = """
____
| |
| 0
| |
|
|
_|_
| |______
| |
|__________|"""
Hangman3 = """
____
| |
| 0
| |
| |
|
_|_
| |______
| |
|__________|"""
Hangman4 = """
____
| |
| 0
| /|
| |
|
_|_
| |______
| |
|__________|"""
Hangman5 = """
____
| |
| 0
| /|\
| |
|
_|_
| |______
| |
|__________|"""
Hangman6 = """
____
| |
| 0
| /|\
| |
| ]
_|_
| |______
| |
|__________|"""
Hangman7 = """
____
| |
| 0
| /|\
| |
| ] ]
_|_
| |______
| |
|__________|"""
alPH = ["a","b","c","d","e","f","g","h","i","j","k","l","m",
"n","o","p","q","r","s","t","u","v","w","x","y","z"]
wordList = ["love and bacon","i eat bananas","potato","odyssey","cats and dogs","heartlands","dingo","omega","mr coffee","family", "mississippi"]
w = random.choice(wordList)
w = w.lower()
blankLst = []
wrongLst = []
def Intro():
animation = "|\-/|\-/|\-/|\-/|\-/|\-/|\-/|\-/|\-/|\-/|\-/|\-/|\-/|\-/|\-/|\-/|\-/|\-/|\-/|\-/|"
print("Loading . . . ")
for i in range(81):
sleep(.1)
sys.stdout.write("\r" + animation[i % len(animation)])
sys.stdout.flush()
if (animation == "|\-/|\-/|\-/|\-/|\-/|\-/|\-/|\-/|\-/|\-/|\-/|\-/|\-/|\-/|\-/|\-/|\-/|\-/|\-/|\-/|"):
print()
print()
Game()
def Game():
for i in w:
if (i != " "):
blankLst.append("_")
else:
blankLst.append(" ")
print("Welcome to Hangman")
print()
sleep(1)
print("Lets Begin!!")
print()
sleep(1)
print(" ".join(blankLst))
print()
sleep(1.5)
tries = 7
while tries != 0:
guess = input("Make a Guess ")
print()
guess = guess.lower()
if guess in w:
for i in range(len(w)):
if (w[i] == guess):
blankLst[i] = w[i]
print(" ".join(blankLst))
sleep(1)
print("Good Job!!!")
elif guess not in w:
sleep(1)
print("Sorry not a letter, you have " +str(tries - 1)+ " tries left. Try again. . . ")
tries -= 1
if (tries == 6):
print(Hangman1)
elif (tries == 5):
print(Hangman2)
elif (tries == 4):
print(Hangman3)
elif (tries == 3):
print(Hangman4)
elif (tries == 2):
print(Hangman5)
elif (tries == 1):
print(Hangman6)
elif (tries == 0):
print(Hangman7)
sleep(1)
lose()
win()
def lose():
print("You lose. . .")
sleep(1)
play = (input("Would you like you play again? ")).lower()
if (play == "y"):
sleep(.75)
Intro()
elif(play == "n"):
print("Dont forget to come back some time.")
def win():
print("You Win!!")
sleep(1)
play = (input("Would you like you play again? ")).lower()
if (play == "y"):
sleep(.75)
Intro()
elif(play == "n"):
print("Dont forget to come back some time.")
def onStart():
print("-~-~-~HANGMAN~-~-~-")
print()
sleep(1.5)
play = (input(""" Start
( y / n )
""")).lower()
if (play == "y"):
sleep(.75)
Intro()
elif(play == "n"):
print("Dont forget to come back some time.")
onStart()
只需在打印后添加
break
(“干得好!!!”)。你不需要任何额外的陈述。1)如果你赢了,就打破while循环(在print(“干得好!!!”)
之后)。2) 避免实际的递归调用。你需要一个主程序来调用各个部分,而不是我怎么做?我可以看出我需要去掉while语句,但是它运行它并在if and or elif语句的末尾停止。如果你猜对了,你永远不会递增trys
,因此你的while
循环永远不会结束,因为这是标准。在循环中需要第二个条件来计算成功的字母,然后使用break
我是否需要使用if():
语句,或者,因为我尝试在while:
和打印(“干得好!!!”)
之后和之前使用if():
。