2013-11-20 20 views
2

THIS IS HOMEWORK 我試圖讓該程序,當用戶說,他們不想再玩了打破的,我不能讓我的劊子手計劃打破,但我無法讓它工作。該程序告訴用戶再次嘗試,但保持相同的單詞,不會中斷。我的循環有什麼問題嗎?如果不是,那麼問題是什麼,我該如何解決?當用戶用完猜測

在此先感謝

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 
+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  
+0

謝謝你們!我真的很感激它,sys.exit()就是我所用的;起初它沒有工作,但我查了一下,得知我必須導入sys,它的功能就像一個魅力 – user3015380

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 
... 

這將徹底結束程序。

相關問題