在Python的for循环中整数被减得太多

2024-06-09 18:57:02 发布

您现在位置:Python中文网/ 问答频道 /正文

这是一个学校的项目,我有一段代码有点问题。 这是一个背景: 我正在制作一个hangman游戏,它运行得很好,但是我正在尝试使循环递减为整数,前提是上面的if语句为false。这似乎是一个问题,因为它减少整数的方式太多,例如,它不是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

Tags: theto代码inforifisit
1条回答
网友
1楼 · 发布于 2024-06-09 18:57:02

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

当前块对randomWord中的每个字母迭代一次,这对后者有效,但对前者无效。你知道吗

分开你的嵌套块。你知道吗

相关问题 更多 >