Interger在Python中的for循环中减少了太多

时间:2016-10-03 20:24:58

标签: python for-loop while-loop

这是一个学校项目,我对一段代码有点麻烦。 这是一个背景: 我正在制作一个刽子手游戏,它工作正常,但我试图让循环减少一个整数,只要它上面的if语句是假的。这似乎是一个问题,因为它过多地递减整数方式,例如,而不是10到9,它将它从10减少到4。 任何帮助表示赞赏。 这是代码:

        # 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

1 个答案:

答案 0 :(得分:1)

在没有看到更多代码的情况下,我认为您将需要一个单独的块来定义播放器输入何时不正确,而不是尝试使用定义播放器输入正确的相同块。

对于randomWord中的每个字母,您当前的块都会迭代一次,后者适用于后者但不适用于前者。

分开你的嵌套块。