当用户用尽猜测时,我无法让我的刽子手程序中断

时间:2013-11-20 23:42:18

标签: python project break

这是家庭作业 当用户说他们不想再玩时,我试图让程序破解,但我无法让它工作。该程序告诉用户再次尝试但保持相同的单词并且不会中断。我的循环有问题吗?如果没有,问题是什么,我该如何解决?

提前致谢

def playGame():
    '''This function allows you to play hangman'''

    print("Welcome to Hangman")
    print("   -------") 
    print("   |     |") 
    print("         |") 
    print("         |") 
    print("         |") 
    print("     -----")
    #def randword(List):
        #wr=random.randint(0,len(List)-1)
        #return List[wr]
    global wordlist
    hangman=open('hangman.txt','r+')
    wordlist=hangman.read()
    x=random.randint(0,len(wordlist)-1)
    w=random.choice(wordlist)
    print (w)
    blanks= '_ ' * len(w)
    #for i in range(len(w)):
       #print('_',sep=" ")
    #print()   
    guesses=[]
    correct=[]
    mistakes=0
    correctguess=0
    guessNumber=0

    while guessNumber<=6:
        print(blanks)
        letter=str(input("Take a guess?"))
        if letter in guesses:
            print("You tried that already! Enter another letter!")
            continue
        if len(letter) > 1:
            print("""Enter ONE letter!""")
        if letter not in guesses:
            guesses.append(letter)
        if letter in w:
            print("You got one, keep going")
            correct.append(letter)
            print (correct)
            for i in range(len(w)):
                if w[i]in correct:
                    correctguess=correctguess+1
                    if len(correct) == len(w):
                        print("You got it! The word was",w,)
                        replay=str(input(""" Want to try again? Enter yes or no"""))
                        if replay=='yes' or replay=='YES' or replay=='Yes':
                            playGame()
                        if replay=='no' or replay=='NO' or replay=='n' or replay=='this game sucked':
                            print("Well, have a nice day then")
                            break
        elif letter not in w:
            mistakes=mistakes+1
            if mistakes == 1: 
                print("   -------") 
                print("   |     |") 
                print("   o     |")
                print("         |") 
                print("         |") 
                print("     -----")
            if mistakes == 2: 
                print("   -------") 
                print("   |     |") 
                print("   o     |") 
                print("   |     |") 
                print("         |") 
                print("     -----") 
            if mistakes == 3: 
                print("   -------") 
                print("   |     |") 
                print("   o     |") 
                print("  /|     |") 
                print("         |") 
                print("     -----") 
            if mistakes == 4: 
                print("   -------") 
                print("   |     |") 
                print("   o     |") 
                print("  /|\    |") 
                print("         |") 
                print("     -----") 
            if mistakes == 5: 
                print("   -------") 
                print("   |     |") 
                print("   o     |") 
                print("  /|\    |") 
                print("  /      |") 
                print("     -----") 
            if mistakes == 6: 
                print("   -------") 
                print("   |     |") 
                print("   o     |") 
                print("  /|\    |") 
                print("  / \    |") 
                print("     -----") 
                print("You did not get the word. Try again!")
                replay=str(input(""" Want to try again? Enter yes or no"""))
                if replay=='yes' or replay=='YES' or replay=='Yes':
                    playGame()
                if replay=='no' or replay=='NO' or replay=='n' or replay=='this game sucked':
                    print("Well, have a nice day then")
                    break                
            print(guesses)
            guessNumber = guessNumber+1

3 个答案:

答案 0 :(得分:1)

我认为您的问题是您以递归方式调用playGame()。当你休息时,你正在回到循环中,可能选择了不同的单词。有几种方法可以处理它。一个是当你想退出时打电话给sys.exit()。第二种方法是传递一个变量,知道你需要退出,并在while循环中检查该变量。或者甚至更好,采取逻辑来决定你是否应该在循环之外再次玩。下面是一个简单的例子。只需用break语句替换您的代码块即可结束游戏,该游戏将下拉到主游戏循环中,如下所示。

while (True):
    playGame()
    replay=str(input(""" Want to try again? Enter yes or no"""))
    replay=replay.lower();   # This line converts to lower case, removing some checks.
    if replay=='no' or or replay=='n' or replay=='this game sucked':
          print("Well, have a nice day then")
          break    

答案 1 :(得分:1)

我认为休息只会从最内层循环中消失。请参阅:http://docs.python.org/2/reference/simple_stmts.html#break

如果你想退出该计划,我相信你所寻找的是sys.exit()

答案 2 :(得分:1)

使用sys.exit()raise SystemExit

... 
print("You did not get the word. Try again!")
replay=str(input(""" Want to try again? Enter yes or no"""))
if replay=='yes' or replay=='YES' or replay=='Yes':
    playGame()
if replay=='no' or replay=='NO' or replay=='n' or replay=='this game sucked':
    print("Well, have a nice day then")
    sys.exit()
    #raise SystemExit
...

这将完全结束该计划。