2016-10-03 88 views
0

這是一個學校項目,我對一段代碼略有麻煩。 這是一個背景: 我正在做一個hang子手遊戲,它工作正常,但我試圖讓循環減少一個整數只有當它上面的if語句是假的。這似乎是一個問題,因爲它減少了整數的方式太多,例如,而不是10到9,它將其從10遞減到4. 任何幫助表示讚賞。 下面是代碼:Interger在Python for循環中遞減得太多

 # This will run when count is above 0 (the length of the word.) 
    while guessesLeft > 0: 
     # Calls letterGuessQuery from the guessing module - gives the user the ability to guess a letter. 
     guessing.letterGuessQuery(guessesLeft,guessedLetters,coveredWord) 
     # Calls letterGuess from the guessing module. 
     guessing.letterGuess(guessedLetters) 


     # Gives the lenght of the randomWord - I use it as an index - I store in it i. 
     for i in range(len(randomWord)): 
      # Loops through randomWord and stores it in x 
      for x in randomWord[i]: 
       # Loops through the guessedLetters. 
       for z in guessedLetters: 
        # If the guessed letter is equal to the letter in random word. 
        if x == z: 
         # Modifies covered word to show the correct letter. 
         coveredWord[i] = x 
        else: 
         guessesLeft -=1 
+4

想想'guessesLeft - = 1'語句運行的次數。 – user2357112

+0

在'guessesLeft - = 1'之後加上'print(「guessesLeft:」,guessesLeft)',你會看到它被執行了多少次。 – furas

+0

哦......我明白了。任何想法如何我可以解決它而不改變整個代碼? – naf

回答

1

沒有看到更多的代碼的我會認爲你會需要一個單獨的塊定義當玩家輸入不正確,而不是試圖用一個定義時相同的塊玩家輸入是正確的。

您當前的程序塊對randomWord中的每個字母都重複一次,後者適用於後者,但不適用於前者。

分開您的嵌套塊。